<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>14. Lösungen &mdash; PowerShell fuer Systemintegratoren</title>
    
    <link rel="stylesheet" href="_static/sphinxdoc.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    './',
        VERSION:     '1.0',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="_static/jquery.js"></script>
    <script type="text/javascript" src="_static/underscore.js"></script>
    <script type="text/javascript" src="_static/doctools.js"></script>
    <script type="text/javascript" src="_static/translations.js"></script>
    <link rel="top" title="PowerShell fuer Systemintegratoren" href="index.html" />
    <link rel="next" title="15. UML" href="powershell_uml.html" />
    <link rel="prev" title="13. Aufgaben" href="Aufgaben.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="Allgemeiner Index"
             accesskey="I">Index</a></li>
        <li class="right" >
          <a href="powershell_uml.html" title="15. UML"
             accesskey="N">weiter</a> |</li>
        <li class="right" >
          <a href="Aufgaben.html" title="13. Aufgaben"
             accesskey="P">zurück</a> |</li>
        <li><a href="index.html">PS4SYS</a> &raquo;</li> 
      </ul>
    </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="index.html">
              <img class="logo" src="_static/PowerShell_icon_small.png" alt="Logo"/>
            </a></p>
  <h3><a href="index.html">Inhalt</a></h3>
  <ul>
<li><a class="reference internal" href="#">14. Lösungen</a><ul>
<li><a class="reference internal" href="#konsole">14.1. Konsole</a></li>
<li><a class="reference internal" href="#editormodus">14.2. Editormodus</a></li>
<li><a class="reference internal" href="#variable-und-datentyp">14.3. Variable_und_Datentyp</a></li>
<li><a class="reference internal" href="#operatoren">14.4. Operatoren</a></li>
<li><a class="reference internal" href="#arrays">14.5. Arrays</a></li>
<li><a class="reference internal" href="#bedingungen">14.6. Bedingungen</a></li>
<li><a class="reference internal" href="#schleifen">14.7. Schleifen</a></li>
<li><a class="reference internal" href="#struktogramm">14.8. Struktogramm</a></li>
<li><a class="reference internal" href="#funktionen">14.9. Funktionen</a></li>
<li><a class="reference internal" href="#pipeline">14.10. Pipeline</a></li>
<li><a class="reference internal" href="#objekte">14.11. Objekte</a><ul>
<li><a class="reference internal" href="#aufgabe-1">14.11.1. Aufgabe 1:</a></li>
<li><a class="reference internal" href="#aufgabe-2">14.11.2. Aufgabe 2:</a></li>
<li><a class="reference internal" href="#aufgabe-3">14.11.3. Aufgabe 3:</a></li>
</ul>
</li>
<li><a class="reference internal" href="#fehlerbehandlung">14.12. Fehlerbehandlung</a></li>
</ul>
</li>
</ul>

  <h4>Vorheriges Thema</h4>
  <p class="topless"><a href="Aufgaben.html"
                        title="vorheriges Kapitel">13. Aufgaben</a></p>
  <h4>Nächstes Thema</h4>
  <p class="topless"><a href="powershell_uml.html"
                        title="nächstes Kapitel">15. UML</a></p>
  <h3>Diese Seite</h3>
  <ul class="this-page-menu">
    <li><a href="_sources/Loesungen.txt"
           rel="nofollow">Quelltext anzeigen</a></li>
  </ul>
<div id="searchbox" style="display: none">
  <h3>Schnellsuche</h3>
    <form class="search" action="search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Los" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Geben Sie Suchbegriffe oder einen Modul-, Klassen- oder Funktionsnamen ein.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="losungen">
<h1>14. Lösungen<a class="headerlink" href="#losungen" title="Permalink zu dieser Überschrift">¶</a></h1>
<div class="section" id="konsole">
<h2>14.1. Konsole<a class="headerlink" href="#konsole" title="Permalink zu dieser Überschrift">¶</a></h2>
</div>
<div class="section" id="editormodus">
<h2>14.2. Editormodus<a class="headerlink" href="#editormodus" title="Permalink zu dieser Überschrift">¶</a></h2>
</div>
<div class="section" id="variable-und-datentyp">
<h2>14.3. Variable_und_Datentyp<a class="headerlink" href="#variable-und-datentyp" title="Permalink zu dieser Überschrift">¶</a></h2>
<ul>
<li><p class="first">Was ist das Ergebnis des folgenden Skriptes</p>
<blockquote>
<div><div class="highlight-sh"><div class="highlight"><pre><span class="o">[</span>int<span class="o">]</span><span class="nv">$a</span> <span class="o">=</span> -7
<span class="o">[</span>int<span class="o">]</span><span class="nv">$b</span> <span class="o">=</span> 3
Write-Host<span class="o">(</span><span class="nv">$a</span>/<span class="nv">$b</span><span class="o">)</span>
Write-Host<span class="o">([</span>int<span class="o">](</span><span class="nv">$a</span>/<span class="nv">$b</span><span class="o">))</span>
-2,3333
-2
</pre></div>
</div>
</div></blockquote>
</li>
</ul>
<p>Das Ergebnis der Division zweier Ganzzahlen ist ein Wert vom Typ double. Erst durch das Casten des Wertes in ein Integer wird eine Ganzzahl als Ergebnis zurückgegeben. Bei Komma 5 wird der Integer-Wert auf die höhere Zahl aufgerundet</p>
<ul>
<li><p class="first">Welche Zeichen sind für das Erstellen eines Variablennamens möglich. Recherchieren Sie falls notwendig auch im Internet</p>
<blockquote>
<div><p>You can use virtually any variable name you choose, names are not case sensitive. Note: there are illegal characters such as; ! &#64; # % &amp; , . and spaces. PowerShell will throw an error if you use an illegal character.</p>
<p>I stated virtually any name as PowerShell does have a set of keywords that are reserved and cannot be used as variable names:</p>
<ul>
<li><dl class="first docutils">
<dt>break</dt>
<dd><ul class="first last simple">
<li>continue</li>
<li>do</li>
<li>else</li>
<li>elseif</li>
<li>filter</li>
<li>foreach</li>
<li>function</li>
<li>if</li>
<li>in</li>
<li>return</li>
<li>switch</li>
<li>until</li>
<li>where</li>
<li>while</li>
</ul>
</dd>
</dl>
</li>
</ul>
</div></blockquote>
</li>
<li><p class="first">Welchen Unterschied macht es, ob sie als Kennzeichen für einen String das Zeichen &#8221; oder das Zeichen &#8216; einsetzen.</p>
<blockquote>
<div><p>Beide Zeichen sind erlaubt, beim &#8216;-Zeichen wird nicht interpoliert. Eine Variable innerhalb des Strings würde nicht mit dem Wert dargestellt. Beispiel:</p>
<div class="highlight-sh"><div class="highlight"><pre><span class="nv">$Teacher1</span> <span class="o">=</span> <span class="s2">&quot;Steinam&quot;</span>
<span class="nv">$TEacher2</span>  <span class="o">=</span> <span class="s1">&#39;Sierl&#39;</span>

<span class="nv">$ausgabe</span> <span class="o">=</span> <span class="s2">&quot;Hallo $Teacher1 &quot;</span>
<span class="nv">$ausgabe2</span> <span class="o">=</span> <span class="s2">&quot;Hallo $Teacher2 &quot;</span>
</pre></div>
</div>
</div></blockquote>
</li>
<li><p class="first">Wie kann man erfahren, welcher Datentyp eine Variable bzw. ein Wert hat</p>
<blockquote>
<div><div class="highlight-sh"><div class="highlight"><pre><span class="o">(</span>42<span class="o">)</span>.GetType<span class="o">()</span>


IsPublic IsSerial Name               BaseType
-------- -------- ----               --------
True     True     Int32              System.ValueType


<span class="o">(</span>-7/3<span class="o">)</span>.GetType<span class="o">()</span>
<span class="o">(</span>9/3<span class="o">)</span>.GetType<span class="o">()</span>
</pre></div>
</div>
</div></blockquote>
</li>
</ul>
<p>Die Werte müssen erst berechnet werden. Dies wird durch die Klammerung erreicht.</p>
<ul>
<li><p class="first">Sie möchten einen String als Datum weiterverwenden, falls er ein reguläres Datum ist.</p>
<blockquote>
<div><div class="highlight-sh"><div class="highlight"><pre><span class="o">[</span>DateTime<span class="o">]</span><span class="s2">&quot;03/13/1964&quot;</span>
</pre></div>
</div>
</div></blockquote>
</li>
<li><p class="first">Welche Vorteile hat das Casten eines Strings in ein DateTime-Objekt</p>
<blockquote>
<div><p>Man verfügt anschließend über die Methoden des DateTime-Objektes, was eine Weiterbearbeitung häufig einfacher macht, z.B. bei Datumsberechnungen.</p>
<div class="highlight-sh"><div class="highlight"><pre><span class="nv">$result</span> <span class="o">=</span> <span class="o">((</span>get-date<span class="o">)</span>.subtract<span class="o">([</span>DateTime<span class="o">]</span><span class="s2">&quot;03/13/1964&quot;</span><span class="o">))</span>
<span class="o">(</span><span class="nv">$result</span>.Days/366<span class="o">)</span>.gettype<span class="o">()</span>
</pre></div>
</div>
</div></blockquote>
</li>
<li><p class="first">Welche Variablen sind beim Start von Powershell bereits vorhanden ? Wie kann man sie sichtbar machen</p>
<blockquote>
<div><div class="highlight-sh"><div class="highlight"><pre>dir variable:
</pre></div>
</div>
<p>zeigt alle in der PS-Sitzung verwendeten bzw. bereits vorhandenen Variablen</p>
</div></blockquote>
</li>
<li><p class="first">Folgender Quellcode ist vorhanden</p>
<blockquote>
<div><div class="highlight-sh"><div class="highlight"><pre><span class="nv">$a</span> <span class="o">=</span> <span class="s2">&quot;Hello&quot;</span>
<span class="nv">$b</span> <span class="o">=</span> 123
</pre></div>
</div>
<ul class="simple">
<li>Bestimmen Sie die Länge des Wertes der Variable $a</li>
<li>An welcher Stelle im String kommt der Buchstabe &#8220;e&#8221; vor</li>
<li>Tauschen Sie den Buchstaben &#8216;e&#8217; durch den Buchstaben &#8216;a&#8217;</li>
</ul>
<div class="highlight-sh"><div class="highlight"><pre><span class="nv">$a</span>.Length

<span class="nv">$a</span>.IndexOf<span class="o">(</span><span class="s2">&quot;e&quot;</span><span class="o">)</span>

<span class="nv">$a</span>.Replace<span class="o">(</span><span class="s2">&quot;e&quot;</span>, <span class="s2">&quot;a&quot;</span><span class="o">)</span>
</pre></div>
</div>
</div></blockquote>
</li>
</ul>
</div>
<div class="section" id="operatoren">
<h2>14.4. Operatoren<a class="headerlink" href="#operatoren" title="Permalink zu dieser Überschrift">¶</a></h2>
<p id="loesung-operatoren">Einfach ein test</p>
</div>
<div class="section" id="arrays">
<h2>14.5. Arrays<a class="headerlink" href="#arrays" title="Permalink zu dieser Überschrift">¶</a></h2>
<ul id="loesung-arrays">
<li><p class="first">Schreiben Sie eine Funktion, die als Parameter einen Pfad erwartet und anschließend den Inhalt des Pfades ausgibt</p>
<blockquote>
<div><div class="highlight-sh"><div class="highlight"><pre><span class="k">function </span>listDirectory<span class="o">(</span><span class="nv">$pfad</span><span class="o">)</span>
<span class="o">{</span>

<span class="nb">cd</span> <span class="nv">$pfad</span>
Dir <span class="nv">$pfad</span> |Out-File  C:<span class="se">\t</span>emp<span class="se">\A</span>usgabe.txt

<span class="o">}</span>
</pre></div>
</div>
</div></blockquote>
</li>
<li><p class="first">Wie überprüfen Sie, ob ein Array einen bestimmten Wert enthält</p>
<blockquote>
<div><p>Mit Hilfe des -contains - Operators</p>
</div></blockquote>
</li>
<li><p class="first">Wie überprüfen Sie, ob zwei Arrays gleich sind</p>
<blockquote>
<div><p>Folgende Funktion überprüft zwei Arrays auf Gleichheit</p>
<div class="highlight-sh"><div class="highlight"><pre><span class="k">function </span>AreArraysEqual<span class="o">(</span><span class="nv">$a1</span>, <span class="nv">$a2</span><span class="o">)</span> <span class="o">{</span>
        <span class="k">if</span> <span class="o">(</span><span class="nv">$a1</span> -isnot <span class="o">[</span>array<span class="o">]</span> -or <span class="nv">$a2</span> -isnot <span class="o">[</span>array<span class="o">])</span> <span class="o">{</span>
          throw <span class="s2">&quot;Both inputs must be an array&quot;</span>
        <span class="o">}</span>
        <span class="k">if</span> <span class="o">(</span><span class="nv">$a1</span>.Rank -ne <span class="nv">$a2</span>.Rank<span class="o">)</span> <span class="o">{</span>
          <span class="k">return</span> <span class="nv">$false</span>
        <span class="o">}</span>
        <span class="k">if</span> <span class="o">([</span>System.Object<span class="o">]</span>::ReferenceEquals<span class="o">(</span><span class="nv">$a1</span>, <span class="nv">$a2</span><span class="o">))</span> <span class="o">{</span>
          <span class="k">return</span> <span class="nv">$true</span>
        <span class="o">}</span>
        <span class="k">for</span> <span class="o">(</span><span class="nv">$r</span> <span class="o">=</span> 0; <span class="nv">$r</span> -lt <span class="nv">$a1</span>.Rank; <span class="nv">$r</span>++<span class="o">)</span> <span class="o">{</span>
          <span class="k">if</span> <span class="o">(</span><span class="nv">$a1</span>.GetLength<span class="o">(</span><span class="nv">$r</span><span class="o">)</span> -ne <span class="nv">$a2</span>.GetLength<span class="o">(</span><span class="nv">$r</span><span class="o">))</span> <span class="o">{</span>
                        <span class="k">return</span> <span class="nv">$false</span>
          <span class="o">}</span>
        <span class="o">}</span>

        <span class="nv">$enum1</span> <span class="o">=</span> <span class="nv">$a1</span>.GetEnumerator<span class="o">()</span>
        <span class="nv">$enum2</span> <span class="o">=</span> <span class="nv">$a2</span>.GetEnumerator<span class="o">()</span>

        <span class="k">while</span> <span class="o">(</span><span class="nv">$enum1</span>.MoveNext<span class="o">()</span> -and <span class="nv">$enum2</span>.MoveNext<span class="o">())</span> <span class="o">{</span>
          <span class="k">if</span> <span class="o">(</span><span class="nv">$enum1</span>.Current -ne <span class="nv">$enum2</span>.Current<span class="o">)</span> <span class="o">{</span>
                        <span class="k">return</span> <span class="nv">$false</span>
          <span class="o">}</span>
        <span class="o">}</span>
        <span class="k">return</span> <span class="nv">$true</span>
<span class="o">}</span>
</pre></div>
</div>
</div></blockquote>
</li>
<li><p class="first">Ein String ist eigentlich ein Array aus Elementen des Datentyps CHAR.</p>
<blockquote>
<div><div class="highlight-sh"><div class="highlight"><pre><span class="nv">$test</span> <span class="o">=</span> <span class="s2">&quot;Hello&quot;</span>
<span class="nv">$test</span><span class="o">[</span>0<span class="o">]</span>.Gettype<span class="o">()</span>

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Char                                     System.ValueType
</pre></div>
</div>
<p>Die Ansprechen eines einzelnen Elementes ist auch mit $test[1] problemlos möglich. Wie können Sie aber ein einzelnes Element eines bestehenden Strings mit Hilfe der Array-Indiziierung ändern, also etwas so</p>
<div class="highlight-sh"><div class="highlight"><pre><span class="nv">$integerarray</span> <span class="o">=</span> 1,2,2
<span class="nv">$integerarray</span><span class="o">[</span>2<span class="o">]</span> <span class="o">=</span> 345  <span class="c"># geht</span>

<span class="nv">$test</span> <span class="o">=</span> <span class="s2">&quot;Hello&quot;</span>
<span class="nv">$test</span><span class="o">[</span>0<span class="o">]</span> <span class="o">=</span> <span class="s2">&quot;F&quot;</span>                  <span class="c">#Fehlermeldung</span>
</pre></div>
</div>
<p><strong>Lösung:</strong></p>
<p>Das Umwandeln mit Hilfe der String-Methode ToCharArray() erzeugt einen Array aus CHAR-Elementen, die verändert werden können.</p>
<div class="highlight-sh"><div class="highlight"><pre><span class="nv">$ergebnis</span> <span class="o">=</span> <span class="nv">$test</span>.ToCharArray<span class="o">()</span>
<span class="nv">$ergebnis</span><span class="o">[</span>0<span class="o">]</span> <span class="o">=</span> <span class="s2">&quot;F&quot;</span>
</pre></div>
</div>
</div></blockquote>
</li>
<li><p class="first">Überprüfen sie, ob ein bestimmter Wert in einem Array enthalten ist, z.B.</p>
<blockquote>
<div><div class="highlight-sh"><div class="highlight"><pre><span class="nv">$arrColors</span> <span class="o">=</span> <span class="s2">&quot;blue&quot;</span>, <span class="s2">&quot;red&quot;</span>, <span class="s2">&quot;green&quot;</span>, <span class="s2">&quot;yellow&quot;</span>, <span class="s2">&quot;white&quot;</span>, <span class="s2">&quot;pink&quot;</span>, <span class="s2">&quot;orange&quot;</span>, <span class="s2">&quot;turquoise&quot;</span>
</pre></div>
</div>
<p>Prüfen Sie, ob der Wert &#8220;green&#8221; enthalten ist</p>
<div class="highlight-sh"><div class="highlight"><pre>foreach<span class="o">(</span><span class="nv">$element</span> in <span class="nv">$arrColors</span><span class="o">)</span>
<span class="o">{</span>
        <span class="k">if</span><span class="o">(</span><span class="nv">$element</span> -eq <span class="s2">&quot;green&quot;</span><span class="o">)</span>
        <span class="o">{</span>
                Write-Host
        <span class="o">}</span>
        <span class="k">else</span>
        <span class="o">{</span>
                Write-Host <span class="s2">&quot;Not found&quot;</span>
        <span class="o">}</span>
<span class="o">}</span>
</pre></div>
</div>
<p>Es geht natürlich in Powershell auch einfacher</p>
<div class="highlight-sh"><div class="highlight"><pre><span class="nv">$arrColors</span> <span class="o">=</span> <span class="s2">&quot;blue&quot;</span>, <span class="s2">&quot;red&quot;</span>, <span class="s2">&quot;green&quot;</span>, <span class="s2">&quot;yellow&quot;</span>, <span class="s2">&quot;white&quot;</span>, <span class="s2">&quot;pink&quot;</span>, <span class="s2">&quot;orange&quot;</span>, <span class="s2">&quot;turquoise&quot;</span>

<span class="nv">$result</span> <span class="o">=</span> <span class="nv">$arrColors</span> -contains <span class="s2">&quot;green&quot;</span>
</pre></div>
</div>
</div></blockquote>
</li>
<li><p class="first">Haben wir Elemente, die mit &#8220;bl&#8221; beginnen</p>
<blockquote>
<div><div class="highlight-sh"><div class="highlight"><pre><span class="nv">$arrColors</span> <span class="o">=</span> <span class="s2">&quot;blue&quot;</span>, <span class="s2">&quot;red&quot;</span>, <span class="s2">&quot;green&quot;</span>, <span class="s2">&quot;yellow&quot;</span>, <span class="s2">&quot;white&quot;</span>, <span class="s2">&quot;pink&quot;</span>, <span class="s2">&quot;orange&quot;</span>, <span class="s2">&quot;turquoise&quot;</span>, <span class="s2">&quot;black&quot;</span>

<span class="nv">$arrColors</span> -like <span class="s2">&quot;bl*&quot;</span>
</pre></div>
</div>
</div></blockquote>
</li>
<li><p class="first">Sortieren sie den obigen Array alphabetisch</p>
<blockquote>
<div><div class="highlight-sh"><div class="highlight"><pre><span class="nv">$arrColors</span> <span class="o">=</span> <span class="s2">&quot;blue&quot;</span>, <span class="s2">&quot;red&quot;</span>, <span class="s2">&quot;green&quot;</span>, <span class="s2">&quot;yellow&quot;</span>, <span class="s2">&quot;white&quot;</span>, <span class="s2">&quot;pink&quot;</span>, <span class="s2">&quot;orange&quot;</span>, <span class="s2">&quot;turquoise&quot;</span>, <span class="s2">&quot;black&quot;</span>

<span class="nv">$arrColors</span> <span class="o">=</span> <span class="nv">$arrColors</span> | Sort-Object
</pre></div>
</div>
</div></blockquote>
</li>
<li><p class="first">Vergleichen Sie die Geschwindigkeit beim Erstellen und Suchen der folgenden Datenstrukturen: Array vs. Hashtable. Welches Ergebnis stellen Sie fest ?</p>
<blockquote>
<div><div class="highlight-sh"><div class="highlight"><pre><span class="nv">$testarray</span> <span class="o">=</span> @<span class="o">()</span>
<span class="nv">$testhash</span> <span class="o">=</span> @<span class="o">{}</span>

Write-Host <span class="s2">&quot;Array anlegen&quot;</span>
Measure-Command <span class="o">{</span>
        <span class="k">for</span><span class="o">(</span><span class="nv">$i</span> <span class="o">=</span> 1; <span class="nv">$i</span> -lt 10000;<span class="nv">$i</span>++<span class="o">)</span>
        <span class="o">{</span>
                <span class="nv">$testarray</span> +<span class="o">=</span> <span class="nv">$i</span>
        <span class="o">}</span>
<span class="o">}</span>

Write-Host <span class="s2">&quot;Hash anlegen&quot;</span>
Measure-Command <span class="o">{</span>
        <span class="k">for</span><span class="o">(</span><span class="nv">$i</span> <span class="o">=</span> 1; <span class="nv">$i</span> -lt 10000;<span class="nv">$i</span>++<span class="o">)</span>
        <span class="o">{</span>
                <span class="nv">$testhash</span>.Add<span class="o">(</span><span class="nv">$i</span>, <span class="nv">$i</span><span class="o">)</span>
        <span class="o">}</span>
<span class="o">}</span>

Write-Host <span class="s2">&quot;Array enthält 9000&quot;</span>
Measure-Command<span class="o">{</span>
        <span class="nv">$testarray</span> -contains 9000
<span class="o">}</span>

Write-Host <span class="s2">&quot;Hash enthält 9000&quot;</span>
Measure-Command<span class="o">{</span>
        <span class="nv">$testhash</span>.get_Item<span class="o">(</span>9000<span class="o">)</span>
<span class="o">}</span>
</pre></div>
</div>
<p>Das Skript erzeugt folgende Ergebnisse auf einem ASUS Netbook (N270, 2 GByte Ram, Windows 7):</p>
<div class="highlight-sh"><div class="highlight"><pre>Array anlegen
...
TotalSeconds      : 27,2567811
TotalMilliseconds : 27256,7811

Hash anlegen
TotalSeconds      : 0,3869414
TotalMilliseconds : 386,9414

Array enthält 9000
.....
TotalSeconds      : 0,0359344
TotalMilliseconds : 35,9344

Hash enthält 9000
...
TotalSeconds      : 0,0001328
TotalMilliseconds : 0,1328
</pre></div>
</div>
<p><strong>Fazit:</strong></p>
<p>Gerade wenn Listen zur Laufzeit wachsen ist ein Array schon bei kleinen Datenmengen relativ langsam. Auch die Suche innerhalb von kleinen Datenmengen wird über eine Hashtable deutlich beschleunigt.</p>
</div></blockquote>
</li>
<li><p class="first">Sie besitzen eine Textdatei mit Familienname und Loginname der Firmenmitarbeiter. Sie umfasst über 9000 Einträge und soll dazu hergenommen werden, um &#8220;vergessene&#8221; Loginnamen zu suchen. Überlegen Sie sich, wie Sie diese Textdatei innerhalb von Powershell-Skripten nutzen können.</p>
</li>
</ul>
<div class="highlight-sh"><div class="highlight"><pre><span class="nv">$fam_login</span> <span class="o">=</span> @<span class="o">{}</span>

Import-Csv user_passwd.csv | ForEach-Object <span class="o">{</span> <span class="nv">$fam_login</span><span class="o">[</span><span class="nv">$_</span>.username<span class="o">]</span> <span class="o">=</span> <span class="nv">$_</span>.passwort<span class="o">}</span>
<span class="nv">$fam_login</span>
<span class="nv">$fam_login</span><span class="o">[</span><span class="s2">&quot;?9d8k6L2&quot;</span><span class="o">]</span>

<span class="nv">$fam_login</span>.GetEnumerator<span class="o">()</span> | <span class="k">select </span>key, value | <span class="nb">export</span>-csv .<span class="se">\t</span>estexport.csv

<span class="nv">$fam_login</span> | <span class="nb">export</span>-clixml -path fam_login.xml


<span class="nv">$fam2</span> <span class="o">=</span> import-clixml -path fam_login.xml
</pre></div>
</div>
<p>Durch die Nutzung der export-clixml und import-clixml - Commandlets werden Dateien im XML-Format geschrieben. Diese machen es einfacher möglich, Metainformationen zu lesen und zu schreiben.</p>
</div>
<div class="section" id="bedingungen">
<span id="loesung-powershell-bedingungen"></span><h2>14.6. Bedingungen<a class="headerlink" href="#bedingungen" title="Permalink zu dieser Überschrift">¶</a></h2>
<ul class="simple" id="loesung-bedingungen">
<li>Mit welchen Vergleichsoperatoren können Sie in Bedingungen arbeiten ?</li>
</ul>
<div class="highlight-sh"><div class="highlight"><pre>-eq,  -ne,  -ge,  -gt,  -lt,  -le,  -like,  -notlike,  -match,  -notmatch,  -contains, -notcontains, -is, -isnot

-and, -or, -xor, -not
</pre></div>
</div>
<ul>
<li><p class="first">Folgende Variablen sind gegeben:</p>
<blockquote>
<div><blockquote>
<div><div class="highlight-sh"><div class="highlight"><pre><span class="nv">$user</span> <span class="o">=</span> <span class="s1">&#39;steinam&#39;</span>
<span class="nv">$pass</span> <span class="o">=</span> <span class="s1">&#39;passwort&#39;</span>
</pre></div>
</div>
</div></blockquote>
<p>Formulieren Sie eine Bedingung in Powershell, die gleichzeitig auf einen korrekten Usernamen und Passwort abfragt.</p>
<div class="highlight-sh"><div class="highlight"><pre><span class="k">if</span><span class="o">(</span><span class="nv">$user</span> -ieq <span class="s1">&#39;STEINAM&#39;</span> -and <span class="nv">$pass</span> -eq <span class="s1">&#39;passwort&#39;</span><span class="o">)</span>
<span class="o">{</span>
        Write-Host <span class="s2">&quot;OK&quot;</span>
<span class="o">}</span>
<span class="k">else</span>
<span class="o">{</span>
        Write-Host <span class="s2">&quot;Fehler&quot;</span>
<span class="o">}</span>
</pre></div>
</div>
</div></blockquote>
</li>
</ul>
<ul>
<li><dl class="first docutils">
<dt>Mit Hilfe von WMI können Sie feststellen, welche Rolle ein Computer innerhalb einer Domäne spielt. Es gibt 6 verschiedene Rollen:</dt>
<dd><ul class="first simple">
<li>0 = Stand alone workstation</li>
<li>1 = Member Workstation</li>
<li>2 = Stand Alone Server</li>
<li>3 = Member Server</li>
<li>4 = Backup Domain Controller</li>
<li>5 = Primary Domain Controller</li>
</ul>
<p>Falls andere Werte zurückkommen, kann die Rolle nicht eindeutig definiert werden</p>
<p>Erstellen Sie ein Skript, welches die jeweilige Rolle in Textform ausgibt.</p>
<div class="last highlight-sh"><div class="highlight"><pre><span class="nv">$wmi</span> <span class="o">=</span> get-wmiobject win32_computersystem
<span class="s2">&quot;computer &quot;</span> + <span class="nv">$wmi</span>.name + <span class="s2">&quot; is: &quot;</span>

switch <span class="o">(</span><span class="nv">$wmi</span>.domainrole<span class="o">)</span>
<span class="o">{</span>
         0 <span class="o">{</span><span class="s2">&quot; Stand alone workstation&quot;</span><span class="o">}</span>
         1 <span class="o">{</span><span class="s2">&quot; Member workstation&quot;</span><span class="o">}</span>
         2 <span class="o">{</span><span class="s2">&quot; Stand alone server&quot;</span><span class="o">}</span>
         3 <span class="o">{</span><span class="s2">&quot; Member server&quot;</span><span class="o">}</span>
         4 <span class="o">{</span><span class="s2">&quot; Back up domain controller&quot;</span><span class="o">}</span>
         5 <span class="o">{</span><span class="s2">&quot; Primary domain controller&quot;</span><span class="o">}</span>
         default <span class="o">{</span><span class="s2">&quot; The role can not be determined&quot;</span><span class="o">}</span>
<span class="o">}</span>
</pre></div>
</div>
</dd>
</dl>
</li>
</ul>
<ul>
<li><p class="first">Drucker auf verschiedenen Betriebssystemen ermitteln</p>
<blockquote>
<div><p>Verschiedene Betriebssysteme nutzen manchmal unterschiedliche WMI Klassen und/oder Eigenschaften, so z.B. Windows XP, Windows 2003 und Windows 2000.
Sie wollen die auf diesen Betriebssystemen verwendeten Drucker auflisten und benötigen ein universell einsetzbares Skript.
Verwenden Sie ein if-Statement zum Ermitteln der korrekten OS-Version und verwenden Sie den dafür vorgesehenen Code.</p>
<p>Benutzen Sie die WMI-Klasse win32_OperatingSystem zum Ermitteln der OS-Version sowie win32_Printer auf XP und 2003 und win32_PrintJob auf Win2000-Systemen</p>
<div class="highlight-sh"><div class="highlight"><pre><span class="nv">$strComputer</span> <span class="o">=</span> Read-Host <span class="s2">&quot;Printer Report – Enter Computer Name&quot;</span>
<span class="nv">$OS</span> <span class="o">=</span> Get-WmiObject -Class win32_OperatingSystem -namespace <span class="s2">&quot;root\CIMV2&quot;</span> -ComputerName <span class="nv">$strComputer</span>

<span class="c"># if statement to run code for Windows XP and Windows 2003 Server.</span>
<span class="k">if</span> <span class="o">((</span><span class="nv">$OS</span>.Version -eq <span class="s2">&quot;5.1.2600&quot;</span><span class="o">)</span> -or <span class="o">(</span><span class="nv">$OS</span>.Version -eq <span class="s2">&quot;5.2.3790&quot;</span><span class="o">))</span>
<span class="o">{</span>
        write-host <span class="s2">&quot;Computer Name: &quot;</span> <span class="nv">$strComputer</span>
        <span class="c">#nested if statement</span>
        <span class="k">if</span> <span class="o">(</span><span class="nv">$OS</span>.Version -eq <span class="s2">&quot;5.1.2600&quot;</span><span class="o">)</span> <span class="o">{</span>write-host <span class="s2">&quot;OS Version: Windows XP&quot;</span><span class="o">}</span>
        elseif <span class="o">(</span><span class="nv">$OS</span>.Version -eq <span class="s2">&quot;5.2.3790&quot;</span><span class="o">)</span> <span class="o">{</span>write-host <span class="s2">&quot;OS Version: Windows 2003&quot;</span><span class="o">}</span>
                <span class="nv">$colPrinters</span> <span class="o">=</span> Get-WmiObject -Class win32_Printer -namespace <span class="s2">&quot;root\CIMV2&quot;</span> -computerName <span class="nv">$strComputer</span>
                        foreach <span class="o">(</span><span class="nv">$objPrinter</span> in <span class="nv">$colPrinters</span><span class="o">)</span> <span class="o">{</span>
                        write-host <span class="s2">&quot;Name: &quot;</span> <span class="nv">$objPrinter</span>.Name
                        write-host <span class="s2">&quot;Description: &quot;</span> <span class="nv">$objPrinter</span>.Description
                        write-host
                        <span class="o">}</span>
<span class="o">}</span>

<span class="c"># if statement to run code for Windows 2000 Server</span>
elseif <span class="o">(</span><span class="nv">$OS</span>.Version -eq <span class="s2">&quot;5.0.2195&quot;</span><span class="o">)</span>
<span class="o">{</span>
        write-host <span class="s2">&quot;Computer Name: &quot;</span> <span class="nv">$strComputer</span>
        write-host <span class="s2">&quot;OS Version: Windows 2000 Server&quot;</span>
                <span class="nv">$colPrinters</span> <span class="o">=</span> Get-WmiObject -Class win32_PrintJob -namespace <span class="s2">&quot;root\CIMV2&quot;</span> -computername <span class="nv">$strComputer</span>
                foreach <span class="o">(</span><span class="nv">$objPrinter</span> in <span class="nv">$colPrinters</span><span class="o">)</span> <span class="o">{</span>
                write-host <span class="s2">&quot;Name: &quot;</span> <span class="nv">$objPrinter</span>.Name
                write-host <span class="s2">&quot;Description: &quot;</span> <span class="nv">$objPrinter</span>.Description
                write-host
                <span class="o">}</span>
<span class="o">}</span>
<span class="c"># if OS not identified</span>
<span class="k">else</span> <span class="o">{</span>write-host <span class="s2">&quot;The OS for: $strComputer is not supported.&quot;</span><span class="o">}</span>
write-host <span class="s2">&quot;–END OF REPORT–&quot;</span>
</pre></div>
</div>
</div></blockquote>
</li>
</ul>
</div>
<div class="section" id="schleifen">
<span id="loesung-powershell-schleifen"></span><h2>14.7. Schleifen<a class="headerlink" href="#schleifen" title="Permalink zu dieser Überschrift">¶</a></h2>
<ul id="loesung-schleifen">
<li><p class="first"><strong>Ping</strong></p>
<blockquote>
<div><p>Schreiben Sie eine Programm, welches Ihnen alle per PING erreichbaren Rechner eines Netzes ermittelt. Das Programm soll alle Adressen von 192.168.0.1 bis 192.168.0.255 pingen. Wenn ein PINg erfolgreich war, soll dies als Ausgabe angezeigt werden.</p>
<div class="highlight-sh"><div class="highlight"><pre><span class="nv">$ping</span> <span class="o">=</span> New-Object System.Net.NetworkInformation.Ping
<span class="nv">$i</span> <span class="o">=</span> 0
<span class="nv">$liste</span> <span class="o">=</span> 1..255
<span class="k">for</span><span class="o">(</span><span class="nv">$y</span><span class="o">=</span>0;<span class="nv">$y</span> -le 255;<span class="nv">$y</span>++<span class="o">)</span>
<span class="o">{</span>
        <span class="nv">$ip</span> <span class="o">=</span> <span class="s2">&quot;192.168.0.&quot;</span> + <span class="o">[</span>string<span class="o">]</span><span class="nv">$liste</span><span class="o">[</span><span class="nv">$y</span><span class="o">]</span>
        <span class="nv">$Res</span> <span class="o">=</span> <span class="nv">$ping</span>.send<span class="o">(</span><span class="nv">$ip</span><span class="o">)</span>
        <span class="k">if</span> <span class="o">(</span><span class="nv">$Res</span>.Status -eq <span class="s2">&quot;Success&quot;</span><span class="o">)</span>
        <span class="o">{</span>
                        <span class="nv">$result</span> <span class="o">=</span> <span class="nv">$ip</span> + <span class="s2">&quot; = Success&quot;</span>
                        Write-Host <span class="nv">$result</span>
                        <span class="nv">$y</span>++
        <span class="o">}</span>
<span class="o">}</span>
<span class="nv">$Hosts</span> <span class="o">=</span> <span class="o">[</span>string<span class="o">]</span><span class="nv">$y</span> + <span class="s2">&quot; Hosts is pingable&quot;</span>
Write-Host <span class="nv">$Hosts</span>

<span class="c">#Eine andere Lösung ist powershell-typischer (per pipeline)</span>

<span class="nv">$ping</span> <span class="o">=</span> New-Object System.Net.NetworkInformation.Ping
<span class="nv">$i</span> <span class="o">=</span> 0
1..255 | foreach
<span class="o">{</span>
        <span class="nv">$ip</span> <span class="o">=</span> <span class="s2">&quot;192.168.0.$_&quot;</span>
        <span class="nv">$Res</span> <span class="o">=</span> <span class="nv">$ping</span>.send<span class="o">(</span><span class="nv">$ip</span><span class="o">)</span>

        <span class="k">if</span> <span class="o">(</span><span class="nv">$Res</span>.Status -eq <span class="s2">&quot;Success&quot;</span><span class="o">)</span>
        <span class="o">{</span>
                <span class="nv">$result</span> <span class="o">=</span> <span class="nv">$ip</span> + <span class="s2">&quot; = Success&quot;</span>
                Write-Host <span class="nv">$result</span>
                <span class="nv">$i</span>++
        <span class="o">}</span>
 <span class="o">}</span>
 <span class="nv">$Hosts</span> <span class="o">=</span> <span class="o">[</span>string<span class="o">]</span><span class="nv">$i</span> + <span class="s2">&quot; Hosts is pingable&quot;</span>
 Write-Host <span class="nv">$Hosts</span>
</pre></div>
</div>
</div></blockquote>
</li>
<li><p class="first"><strong>ZahlenRaten</strong></p>
<p>Erstellen sie ein Skript, welches folgende Aufgabe erfüllt: Es muss eine Meldung anzeigen, in der der Benutzer aufgefordert wird, eine Zahl zwischen 1 und 50 einzugebeenn. Das Skript muss die vom Benutzer eingegebene Zahl mit einer zufällig generierten Zahl vergleichen. Wenn die Zahlen nicht übereinstimmen, muss das Skript eine Meldung anzeigen, in der angegeben wird, ob die geratene Zahl zu hoch oder zu niedrig war, und der Benutzer aufgefordert wird, noch einmal zu raten.</p>
<p>Wenn der Benutzer richtig rät, muss das Skript die Zufallszahl sowie die Anzahl der Rateversuche anzeigen. An diesem Punkt ist das Spiel beendet, das Skript muss also auch beendet werden.</p>
<div class="highlight-sh"><div class="highlight"><pre><span class="nv">$guesses</span> <span class="o">=</span> 0

<span class="nv">$low</span> <span class="o">=</span> 1
<span class="nv">$high</span> <span class="o">=</span> 50

<span class="nv">$a</span> <span class="o">=</span> New-Object Random
<span class="nv">$a</span> <span class="o">=</span> <span class="nv">$a</span>.Next<span class="o">(</span><span class="nv">$low</span>,<span class="nv">$high</span><span class="o">)</span>

<span class="k">while</span> <span class="o">(</span><span class="nv">$true</span><span class="o">)</span>
<span class="o">{</span>
        <span class="nv">$guess</span> <span class="o">=</span> <span class="nb">read</span>-host <span class="s2">&quot;Enter a number between 1 and 50: &quot;</span>
        <span class="nv">$guesses</span>++

        <span class="k">if</span> <span class="o">(</span><span class="nv">$guess</span> -eq <span class="nv">$a</span><span class="o">)</span>
        <span class="o">{</span>
                        <span class="s2">&quot;Random Number: &quot;</span> + <span class="nv">$guess</span>
                        <span class="s2">&quot;Number of Guesses: &quot;</span> + <span class="nv">$guesses</span>
                        <span class="nb">break</span>
        <span class="o">}</span>
        elseif <span class="o">(</span><span class="nv">$guess</span> -gt <span class="nv">$a</span><span class="o">)</span>
        <span class="o">{</span>
                        <span class="s2">&quot;Too high&quot;</span>
        <span class="o">}</span>
        <span class="k">else</span>
        <span class="o">{</span>
                        <span class="s2">&quot;Too low&quot;</span>
        <span class="o">}</span>
<span class="o">}</span>
</pre></div>
</div>
<img alt="_images/zahlenraten.png" src="_images/zahlenraten.png" />
</li>
<li><p class="first"><strong>Dateien kopieren</strong></p>
<blockquote>
<div><p>Diese Skripts sollen folgende Aufgaben ausführen:</p>
<ul class="simple">
<li>Durchsuchen des Ordners &#8220;C:Scripts&#8221; und dessen Unterordnern.</li>
<li>Durchsuchen jedes Ordners nach sämtlichen Textdateien (Dateien mit der Erweiterung .txt) und Prüfen des Erstellungsdatums jeder Datei.</li>
<li>Kopieren/Verschieben jeder .txt-Datei, die mehr als 10 Tage zuvor erstellt wurde, in den Ordner &#8220;C:Old&#8221;.</li>
<li>Ausgeben des Dateinamens (kein vollständiger Pfad, nur Dateiname) jeder kopierten Datei.</li>
<li>Ausgeben der Anzahl der kopierten Dateien.</li>
</ul>
<div class="highlight-sh"><div class="highlight"><pre>foreach <span class="o">(</span><span class="nv">$i</span> in Get-ChildItem C:<span class="se">\S</span>cripts -recurse<span class="o">)</span>
<span class="o">{</span>
        <span class="k">if</span> <span class="o">((</span><span class="nv">$i</span>.CreationTime -lt <span class="o">(</span><span class="k">$(</span>Get-Date<span class="k">)</span>.AddDays<span class="o">(</span>-10<span class="o">)))</span> -and <span class="o">(</span><span class="nv">$i</span>.Extension -eq <span class="s2">&quot;.txt&quot;</span><span class="o">))</span>
        <span class="o">{</span>
                <span class="c">#Copy-Item $i.FullName C:\old</span>
                <span class="nv">$i</span>.Name
                <span class="nv">$x</span> <span class="o">=</span> <span class="nv">$x</span> + 1
        <span class="o">}</span>
<span class="o">}</span>
<span class="s2">&quot;Total Files: &quot;</span> + <span class="nv">$x</span>
</pre></div>
</div>
</div></blockquote>
</li>
<li><p class="first"><strong>Fonts finden</strong></p>
</li>
</ul>
<div class="highlight-sh"><div class="highlight"><pre><span class="nv">$total</span> <span class="o">=</span> 0

<span class="nv">$a</span> <span class="o">=</span> get-item <span class="s2">&quot;hklm:\\Software\Microsoft\WindowsNT\CurrentVersion\Fonts&quot;</span>
<span class="nv">$f</span> <span class="o">=</span> <span class="nv">$a</span>.GetValueNames<span class="o">()</span>

foreach <span class="o">(</span><span class="nv">$i</span> in <span class="nv">$f</span><span class="o">)</span>
<span class="o">{</span>
        <span class="k">if</span> <span class="o">(</span><span class="nv">$i</span>.contains<span class="o">(</span><span class="s2">&quot;TrueType&quot;</span><span class="o">))</span>
        <span class="o">{</span>
                <span class="nv">$total</span>++
                <span class="nv">$i</span>
        <span class="o">}</span>
<span class="o">}</span>
</pre></div>
</div>
<ul>
<li><p class="first"><strong>Schleifen optimieren</strong></p>
<blockquote>
<div><p>Die Ausgabe des folgenden Powershell-Befehls dauert u.U. sehr lange. Verbessern Sie das Statement im Hinblick auf die Auführungsgeschwindigkeit</p>
<div class="highlight-sh"><div class="highlight"><pre><span class="c"># Foreach loop lists each element in a collection:</span>
Foreach <span class="o">(</span><span class="nv">$element</span> in Dir C:<span class="se">\ </span>-recurse<span class="o">)</span> <span class="o">{</span> <span class="nv">$element</span>.name <span class="o">}</span>
</pre></div>
</div>
<p>Lösung: Ersetze Foreach durch das ForEach-Objekt, das die Elemente der Pipeline sofort bearbeiten kann.</p>
<div class="highlight-sh"><div class="highlight"><pre><span class="c"># ForEach-Object lists each element in a pipeline:</span>
Dir C:<span class="se">\ </span>-recurse | ForEach-Object <span class="o">{</span> <span class="nv">$_</span>.name <span class="o">}</span>
</pre></div>
</div>
</div></blockquote>
</li>
<li><p class="first"><strong>Dateien per ftp hochladen</strong></p>
<blockquote>
<div><p>Sie sollen alle Dateien eines zu spezifizierenden Ordners auf einen ftp-server hochladen. Übergeben Sie den Ordner auf der Kommandozeile</p>
<div class="highlight-sh"><div class="highlight"><pre><span class="c">#we specify the directory where all files that we want to upload</span>
<span class="c">#$Dir=&quot;C:/Dir&quot;</span>

<span class="k">if</span><span class="o">(</span><span class="nb">test</span>-path <span class="nv">$args</span><span class="o">[</span>0<span class="o">])</span>
<span class="o">{</span>

        <span class="nv">$Dir</span><span class="o">=</span><span class="nv">$args</span><span class="o">[</span>0<span class="o">]</span>

        <span class="c">#ftp server</span>
        <span class="nv">$ftp</span> <span class="o">=</span> <span class="s2">&quot;ftp://ftp.server.com/dir/&quot;</span>
        <span class="nv">$user</span> <span class="o">=</span> <span class="s2">&quot;user&quot;</span>
        <span class="nv">$pass</span> <span class="o">=</span> <span class="s2">&quot;Pass&quot;</span>

        <span class="nv">$webclient</span> <span class="o">=</span> New-Object System.Net.WebClient

        <span class="nv">$webclient</span>.Credentials <span class="o">=</span> New-Object System.Net.NetworkCredential<span class="o">(</span><span class="nv">$user</span>,<span class="nv">$pass</span><span class="o">)</span>

        <span class="c">#list every sql server trace file</span>
        foreach<span class="o">(</span><span class="nv">$item</span> in <span class="o">(</span>dir <span class="nv">$Dir</span> <span class="s2">&quot;*.trc&quot;</span><span class="o">)){</span>
                <span class="s2">&quot;Uploading $item...&quot;</span>
                <span class="nv">$uri</span> <span class="o">=</span> New-Object System.Uri<span class="o">(</span><span class="nv">$ftp</span>+<span class="nv">$item</span>.Name<span class="o">)</span>
                <span class="nv">$webclient</span>.UploadFile<span class="o">(</span><span class="nv">$uri</span>, <span class="nv">$item</span>.FullName<span class="o">)</span>
         <span class="o">}</span>
<span class="o">}</span>
</pre></div>
</div>
<p>Unter-Windows kann man auch das BitsTransfer-Cmdlet benutzen.
Muss mit Import-Module BitsTransfer in die PS geladen werden (PS V. 2.0). Siehe dazu auch <a class="reference external" href="http://msdn.microsoft.com/en-us/library/ee663885%28v=VS.85%29.aspx">http://msdn.microsoft.com/en-us/library/ee663885%28v=VS.85%29.aspx</a></p>
</div></blockquote>
</li>
<li><p class="first"><strong>Übersicht über laufende Dienste</strong></p>
<blockquote>
<div><p>Verschaffen Sie sich einen Überblick über die laufenden Prozesse. Falls die Prozesse einen bestimmten Namen besitzen, stoppen Sie diese Prozesse bzw. geben das Wort &#8220;Gefunden &lt;Dienstname&gt; aus.</p>
<p>Erweitern Sie das Skript, indem Sie die laufenden Dienste in eine Datei schreiben, um sie später wie oben bereits geschehen, auszuwerten. Welche Schwierigkeiten können bei diesem Vorhaben auftreten.</p>
<div class="highlight-sh"><div class="highlight"><pre>Get-Service | ForEach-Object <span class="o">{</span>
        <span class="k">if</span><span class="o">(</span><span class="nv">$_</span>.ServiceName -match <span class="s2">&quot;RpcSs&quot;</span><span class="o">)</span>
        <span class="o">{</span>
                Stop-Service <span class="nv">$_</span>.ServiceName
                Write-Host <span class="s2">&quot;Gefunden: &quot;</span>  <span class="nv">$_</span>.Name
        <span class="o">}</span>
<span class="o">}</span>

Get-Service | Export-Clixml C:<span class="se">\d</span>ienste.xml
foreach <span class="o">(</span><span class="nv">$service</span> in Import-Clixml C:<span class="se">\d</span>ienste.xml<span class="o">)</span>
<span class="o">{</span>
        <span class="k">if</span><span class="o">(</span><span class="nv">$service</span>.ServiceName -match <span class="s2">&quot;RpcSs&quot;</span><span class="o">)</span>
        <span class="o">{</span>
                Write-Host <span class="s2">&quot;Gefunden: &quot;</span>  <span class="nv">$service</span>.Name
        <span class="o">}</span>
<span class="o">}</span>
</pre></div>
</div>
</div></blockquote>
</li>
<li><p class="first"><strong>Schätzen der Festplattenauslastung</strong></p>
<blockquote>
<div><p>Wie lange wird der Speicherplatz einer Festplatte ausreichen, wenn ihr Inhalt jeden Monat um ca. 7,5% wächst. Die Platte hat eine Kapazität von 2 TBiT, eine Startbelegung von 100 MBiT</p>
<div class="highlight-sh"><div class="highlight"><pre><span class="nv">$Zuwachs</span> <span class="o">=</span> 7.5
<span class="nv">$FestplatteMax</span> <span class="o">=</span> 2TB
<span class="nv">$StartKap</span> <span class="o">=</span> 100MB
<span class="nv">$FestplatteCurKap</span> <span class="o">=</span> <span class="nv">$StartKap</span>
<span class="nv">$i</span> <span class="o">=</span> 0
<span class="k">do</span>
<span class="o">{</span>

        <span class="nv">$FestplatteCurKap</span> *<span class="o">=</span> 1 + <span class="nv">$Zuwachs</span>/100
        <span class="nv">$i</span> +<span class="o">=</span> 1
        Write-Host <span class="s2">&quot;Monat: &quot;</span> <span class="nv">$i</span>

<span class="o">}</span><span class="k">while</span><span class="o">(</span><span class="nv">$FestplatteCurKap</span> -lt <span class="nv">$FestplatteMax</span><span class="o">)</span>

<span class="nv">$result</span> <span class="o">=</span> Get-Date
<span class="nv">$result</span> <span class="o">=</span> <span class="nv">$result</span>.AddMonths<span class="o">(</span><span class="nv">$i</span><span class="o">)</span>
<span class="nv">$result</span>
</pre></div>
</div>
</div></blockquote>
</li>
<li><p class="first"><strong>Berechnung des Notendurchnschnitts und Notenverteilung eines Testes</strong></p>
<blockquote>
<div><p>Ein Test ergab folgende Einzelnoten:
6,3,4,2,1,2,3,4,1,2,3,2,1,3,4,5,3,5,4,3,2,2,2,1,2,3</p>
<p>Berechnen Sie die Anzahl jeder Note sowie den Durchschnitt</p>
<p>Die folgende Lösung setzt in einigen Teilen die Pipeline der Powershell ein. Dies hätte auch durch eine weitere Schleife erledigt werden können.
Bei der Zuweisung der Häufigkeiten pro Noten wird ein 2. Array eingesetzt, ders ich pro Indexposition (ab 1) die absolute Zahl der jeweiligen Note merkt. In Indexposition 0 wird später der Durchschnitt abgelegt</p>
<div class="highlight-sh"><div class="highlight"><pre><span class="nv">$noten_sa1</span> <span class="o">=</span> 6,3,4,2,1,2,3,4,1,2,3,2,1,3,4,5,3,5,4,3,2,2,2,1,2,3
<span class="nv">$notenwerte</span> <span class="o">=</span> 1,2,3,4,5,6
<span class="nv">$notenstat</span> <span class="o">=</span> 0,0,0,0,0,0,0

1..6 | ForEach<span class="o">{</span>
        <span class="nv">$anzahl</span> <span class="o">=</span> 0
        <span class="k">for</span><span class="o">(</span><span class="nv">$i</span> <span class="o">=</span> 0;<span class="nv">$i</span> -lt <span class="nv">$noten_sa1</span>.length; <span class="nv">$i</span>++<span class="o">)</span>
        <span class="o">{</span>
                <span class="k">if</span><span class="o">(</span><span class="nv">$noten_sa1</span><span class="o">[</span><span class="nv">$i</span><span class="o">]</span> -eq <span class="nv">$_</span><span class="o">)</span>
                <span class="o">{</span>
                        Write-Host <span class="s2">&quot;Note &quot;</span>  <span class="nv">$_</span> <span class="s2">&quot;vorhanden an Position &quot;</span> + <span class="o">(</span><span class="nv">$i</span> + 1<span class="o">)</span>
                        <span class="nv">$anzahl</span>++
                <span class="o">}</span>
                <span class="k">else</span>
                <span class="o">{</span>
                        Write-Host <span class="s2">&quot;Note nicht vorhanden&quot;</span>
                <span class="o">}</span>
        <span class="o">}</span>
        <span class="nv">$notenstat</span><span class="o">[</span><span class="nv">$_</span><span class="o">]</span> <span class="o">=</span> <span class="nv">$anzahl</span>

<span class="o">}</span>
Write-Host <span class="nv">$notenstat</span>

<span class="nv">$durchschnitt</span> <span class="o">=</span> 0
<span class="nv">$quersumme</span><span class="o">=</span>0
<span class="k">for</span> <span class="o">(</span><span class="nv">$i</span> <span class="o">=</span> 1; <span class="nv">$i</span> -lt <span class="nv">$notenstat</span>.length;<span class="nv">$i</span>++<span class="o">)</span>
<span class="o">{</span>
        <span class="nv">$quersumme</span> +<span class="o">=</span> <span class="nv">$notenstat</span><span class="o">[</span><span class="nv">$i</span><span class="o">]</span>*<span class="nv">$i</span>
<span class="o">}</span>
<span class="nv">$durchschnitt</span> <span class="o">=</span> <span class="nv">$quersumme</span> / <span class="nv">$noten_sa1</span>.length
<span class="nv">$notenstat</span><span class="o">[</span>0<span class="o">]</span> <span class="o">=</span> <span class="nv">$durchschnitt</span>
<span class="nv">$quersumme</span>
<span class="nv">$durchschnitt</span>
</pre></div>
</div>
<p>Eine weitere und vielleicht elegantere Möglichkeit wäre die Verwendung einer Hashmap anstelle des Arrays $notenstat. Damit müsste man die Indexpositionen nicht <em>missbrauchen</em>, sondern könnte die Noten als Key und die Anzahl als Values einsetzen.</p>
<p>Eine Lösungsansatz:</p>
<div class="highlight-sh"><div class="highlight"><pre><span class="nv">$noten_sa1</span> <span class="o">=</span> 6,3,4,2,1,2,3,4,1,2,3,2,1,3,4,5,3,5,4,3,2,2,2,1,2,3
<span class="nv">$notenwerte</span> <span class="o">=</span> 1,2,3,4,5,6
<span class="nv">$notenhash</span> <span class="o">=</span> @<span class="o">{}</span>

1..6 | ForEach<span class="o">{</span>
        <span class="nv">$anzahl</span> <span class="o">=</span> 0
        <span class="k">for</span><span class="o">(</span><span class="nv">$i</span> <span class="o">=</span> 0;<span class="nv">$i</span> -lt <span class="nv">$noten_sa1</span>.length; <span class="nv">$i</span>++<span class="o">)</span>
        <span class="o">{</span>
                <span class="k">if</span><span class="o">(</span><span class="nv">$noten_sa1</span><span class="o">[</span><span class="nv">$i</span><span class="o">]</span> -eq <span class="nv">$_</span><span class="o">)</span>
                <span class="o">{</span>
                        <span class="c">#Write-Host &quot;Note &quot;  $_ &quot;vorhanden an Position &quot; + ($i + 1)</span>
                        <span class="nv">$anzahl</span>++
                <span class="o">}</span>
                <span class="k">else</span>
                <span class="o">{</span>
                        <span class="c"># Write-Host &quot;Note nicht vorhanden&quot;</span>
                <span class="o">}</span>
        <span class="o">}</span>
        <span class="nv">$notenhash</span>.Add<span class="o">(</span><span class="nv">$_</span>, <span class="nv">$anzahl</span><span class="o">)</span>
<span class="o">}</span>

<span class="nv">$durchschnitt</span> <span class="o">=</span> 0
<span class="nv">$quersumme</span> <span class="o">=</span> 0
foreach<span class="o">(</span><span class="nv">$key</span> in <span class="nv">$notenhash</span>.get_Keys<span class="o">())</span>
<span class="o">{</span>
        Write-Host <span class="nv">$key</span> <span class="nv">$notenhash</span><span class="o">[</span><span class="nv">$key</span><span class="o">]</span>
        <span class="nv">$quersumme</span> +<span class="o">=</span> <span class="nv">$key</span> * <span class="nv">$notenhash</span><span class="o">[</span><span class="nv">$key</span><span class="o">]</span>
<span class="o">}</span>
<span class="nv">$durchschnitt</span> <span class="o">=</span> <span class="nv">$quersumme</span> / <span class="nv">$noten_sa1</span>.length
<span class="nv">$durchschnitt</span>
</pre></div>
</div>
</div></blockquote>
</li>
</ul>
</div>
<div class="section" id="struktogramm">
<h2>14.8. Struktogramm<a class="headerlink" href="#struktogramm" title="Permalink zu dieser Überschrift">¶</a></h2>
</div>
<div class="section" id="funktionen">
<span id="loesung-powershell-funktionen"></span><h2>14.9. Funktionen<a class="headerlink" href="#funktionen" title="Permalink zu dieser Überschrift">¶</a></h2>
<ul id="loesung-dir">
<li><p class="first">Schreiben Sie eine Funktion, die als Parameter einen Pfad erwartet und anschließend den Inhalt des Pfades ausgibt</p>
<blockquote>
<div><div class="highlight-sh"><div class="highlight"><pre><span class="k">function </span>listDirectory<span class="o">(</span><span class="nv">$pfad</span><span class="o">)</span>
<span class="o">{</span>

<span class="nb">cd</span> <span class="nv">$pfad</span>
Dir <span class="nv">$pfad</span> |Out-File  C:<span class="se">\t</span>emp<span class="se">\A</span>usgabe.txt

<span class="o">}</span>
</pre></div>
</div>
</div></blockquote>
</li>
</ul>
<ul id="loesung-dir-rekursiv">
<li><p class="first">Erweitern Sie die obige Funktion um eine rekursive Variante. Sollte der Pfad Unterordner besitzen, sollen auch diese ausgegeben werden.</p>
<blockquote>
<div><div class="highlight-sh"><div class="highlight"><pre><span class="k">function </span>listDirectory<span class="o">(</span><span class="nv">$pfad</span><span class="o">)</span>
<span class="o">{</span>

<span class="nb">cd</span> <span class="nv">$pfad</span>
Dir <span class="nv">$pfad</span> -recurse |Out-File  C:<span class="se">\t</span>emp<span class="se">\A</span>usgabe.txt

<span class="o">}</span>
</pre></div>
</div>
</div></blockquote>
</li>
</ul>
<ul id="loesung-dir-rekursiv-extension">
<li><p class="first">Erweitern Sie die obige Funktion: Es soll jetzt möglich sein, nach bestimmten Dateikennungen die Ausgabe zu gestalten</p>
<blockquote>
<div><div class="highlight-sh"><div class="highlight"><pre><span class="k">function </span>listDirectory<span class="o">(</span><span class="nv">$pfad</span>, <span class="nv">$ext</span><span class="o">)</span>
<span class="o">{</span>
        <span class="nb">cd</span> <span class="nv">$pfad</span>
        Dir <span class="nv">$pfad</span> -Recurse  -include *.<span class="nv">$ext</span> |Out-File  C:<span class="se">\t</span>emp<span class="se">\A</span>usgabe.txt
<span class="o">}</span>
</pre></div>
</div>
</div></blockquote>
</li>
<li><p class="first">Wie überprüfen Sie, ob ein Array einen bestimmten Wert enthält</p>
<blockquote>
<div><p>Folgende Funktion überprüft zwei Arrays auf Gleichheit</p>
<div class="highlight-sh"><div class="highlight"><pre><span class="k">function </span>AreArraysEqual<span class="o">(</span><span class="nv">$a1</span>, <span class="nv">$a2</span><span class="o">)</span> <span class="o">{</span>
        <span class="k">if</span> <span class="o">(</span><span class="nv">$a1</span> -isnot <span class="o">[</span>array<span class="o">]</span> -or <span class="nv">$a2</span> -isnot <span class="o">[</span>array<span class="o">])</span> <span class="o">{</span>
          throw <span class="s2">&quot;Both inputs must be an array&quot;</span>
        <span class="o">}</span>
        <span class="k">if</span> <span class="o">(</span><span class="nv">$a1</span>.Rank -ne <span class="nv">$a2</span>.Rank<span class="o">)</span> <span class="o">{</span>
          <span class="k">return</span> <span class="nv">$false</span>
        <span class="o">}</span>
        <span class="k">if</span> <span class="o">([</span>System.Object<span class="o">]</span>::ReferenceEquals<span class="o">(</span><span class="nv">$a1</span>, <span class="nv">$a2</span><span class="o">))</span> <span class="o">{</span>
          <span class="k">return</span> <span class="nv">$true</span>
        <span class="o">}</span>
        <span class="k">for</span> <span class="o">(</span><span class="nv">$r</span> <span class="o">=</span> 0; <span class="nv">$r</span> -lt <span class="nv">$a1</span>.Rank; <span class="nv">$r</span>++<span class="o">)</span> <span class="o">{</span>
          <span class="k">if</span> <span class="o">(</span><span class="nv">$a1</span>.GetLength<span class="o">(</span><span class="nv">$r</span><span class="o">)</span> -ne <span class="nv">$a2</span>.GetLength<span class="o">(</span><span class="nv">$r</span><span class="o">))</span> <span class="o">{</span>
                        <span class="k">return</span> <span class="nv">$false</span>
          <span class="o">}</span>
        <span class="o">}</span>

        <span class="nv">$enum1</span> <span class="o">=</span> <span class="nv">$a1</span>.GetEnumerator<span class="o">()</span>
        <span class="nv">$enum2</span> <span class="o">=</span> <span class="nv">$a2</span>.GetEnumerator<span class="o">()</span>

        <span class="k">while</span> <span class="o">(</span><span class="nv">$enum1</span>.MoveNext<span class="o">()</span> -and <span class="nv">$enum2</span>.MoveNext<span class="o">())</span> <span class="o">{</span>
          <span class="k">if</span> <span class="o">(</span><span class="nv">$enum1</span>.Current -ne <span class="nv">$enum2</span>.Current<span class="o">)</span> <span class="o">{</span>
                        <span class="k">return</span> <span class="nv">$false</span>
          <span class="o">}</span>
        <span class="o">}</span>
        <span class="k">return</span> <span class="nv">$true</span>
<span class="o">}</span>
</pre></div>
</div>
</div></blockquote>
</li>
<li><p class="first">Folgendes Powershell-Skript berechnet den Wochentag mit Hilfe eines eigenen Algorythmus. Verbessern Sie die Lesbarkeit des Skriptes, indem Sie die verschiedenen Teile des Quellcode mit Hilfe von Funktionen kapseln.</p>
<blockquote>
<div><div class="highlight-sh"><div class="highlight"><pre>&lt;<span class="c">#</span>
.SYNOPSIS
Fragt nach dem Teil eines Datums

.DESCRIPTION
Dient zum Erfassen der Bestandteile eines Datums. Macht keine Sicherheitsüberprüfungen

.PARAMETER Frage
Specifiziert die dem User zu stellende Frage.

.OUTPUTS
System.String. Gibt die von der Eingabeaufforderung übergebenen Zeichen zurück

.LINK
Read-Host
<span class="c">#&gt;</span>
<span class="k">function </span>get-DatumsTeil<span class="o">([</span>string<span class="o">]</span><span class="nv">$Frage</span><span class="o">)</span>
<span class="o">{</span>
        <span class="k">return </span>Read-Host -prompt <span class="nv">$Frage</span>
<span class="o">}</span>


<span class="k">function </span><span class="nb">set</span>-Monat<span class="o">()</span>
<span class="o">{</span>
        <span class="k">if</span><span class="o">(</span><span class="nv">$m</span> -le 2<span class="o">)</span>
        <span class="o">{</span>
           <span class="nv">$m</span> +<span class="o">=</span> 10;
                <span class="nv">$j</span> -<span class="o">=</span> 1;
        <span class="o">}</span>
        <span class="k">else</span>
        <span class="o">{</span>
           <span class="nv">$m</span> -<span class="o">=</span> 2;
        <span class="o">}</span>

        <span class="o">[</span>int<span class="o">]</span><span class="nv">$c</span> <span class="o">=</span> <span class="nv">$j</span>/100;
        <span class="o">[</span>int<span class="o">]</span><span class="nv">$y</span> <span class="o">=</span> <span class="nv">$j</span>%100;

        <span class="o">[</span>int<span class="o">]</span><span class="nv">$h</span> <span class="o">=</span> <span class="o">(((</span>26 * <span class="nv">$m</span> -2<span class="o">)</span>/10<span class="o">)</span> + <span class="nv">$t</span> + <span class="nv">$y</span> + <span class="nv">$y</span>/4 + <span class="nv">$c</span>/4 -2 * <span class="nv">$c</span><span class="o">)</span>%7

        <span class="k">if</span><span class="o">(</span><span class="nv">$h</span> -lt 0<span class="o">)</span>
        <span class="o">{</span>
                <span class="nv">$h</span> +<span class="o">=</span>7;
        <span class="o">}</span>

        <span class="k">return</span> <span class="nv">$h</span>
<span class="o">}</span>


<span class="k">function </span>get-Day<span class="o">(</span><span class="nv">$value</span><span class="o">)</span>
<span class="o">{</span>
   <span class="o">[</span>string<span class="o">]</span><span class="nv">$Tag</span> <span class="o">=</span> <span class="s2">&quot;&quot;</span>;
        switch<span class="o">(</span><span class="nv">$h</span><span class="o">)</span>
        <span class="o">{</span>
           0 <span class="o">{</span> <span class="nv">$Tag</span> <span class="o">=</span> <span class="s2">&quot;Sonntag&quot;</span>; <span class="nb">break</span><span class="o">}</span>

           1 <span class="o">{</span> <span class="nv">$Tag</span> <span class="o">=</span> <span class="s2">&quot;Montag&quot;</span>; <span class="nb">break</span><span class="o">}</span>

           2 <span class="o">{</span> <span class="nv">$Tag</span> <span class="o">=</span> <span class="s2">&quot;Dienstag&quot;</span>; <span class="nb">break</span><span class="o">}</span>

           3 <span class="o">{</span> <span class="nv">$Tag</span> <span class="o">=</span> <span class="s2">&quot;Mittwoch&quot;</span>; <span class="nb">break</span><span class="o">}</span>

           4 <span class="o">{</span> <span class="nv">$Tag</span> <span class="o">=</span> <span class="s2">&quot;Donnerstag&quot;</span>;<span class="nb">break</span><span class="o">}</span>

           5 <span class="o">{</span> <span class="nv">$Tag</span> <span class="o">=</span> <span class="s2">&quot;Freitag&quot;</span>; <span class="nb">break</span><span class="o">}</span>

           6 <span class="o">{</span> <span class="nv">$Tag</span> <span class="o">=</span> <span class="s2">&quot;Samstag&quot;</span>; <span class="nb">break</span><span class="o">}</span>
        <span class="o">}</span>

        <span class="k">return</span> <span class="nv">$Tag</span>
<span class="o">}</span>


<span class="nv">$t</span> <span class="o">=</span> get-DatumsTeil<span class="o">(</span><span class="s2">&quot;Geben Sie den Tag ein&quot;</span><span class="o">)</span>
<span class="nv">$m</span> <span class="o">=</span> get-DatumsTeil<span class="o">(</span><span class="s2">&quot;Geben Sie den  Monat ein&quot;</span><span class="o">)</span>
<span class="nv">$j</span> <span class="o">=</span> get-DatumsTeil<span class="o">(</span><span class="s2">&quot;Geben Sie das Jahr ein&quot;</span><span class="o">)</span>

<span class="nv">$MerkeMonat</span> <span class="o">=</span> <span class="nv">$m</span>
<span class="nv">$h</span> <span class="o">=</span> <span class="nb">set</span>-Monat
<span class="nv">$Tag</span> <span class="o">=</span> get-Day<span class="o">(</span><span class="nv">$h</span><span class="o">)</span>
Write-Host <span class="s2">&quot;Der &quot;</span> + <span class="nv">$t</span> +<span class="s2">&quot;.&quot;</span> + <span class="nv">$MerkeMonat</span> + <span class="s2">&quot;.&quot;</span> + <span class="nv">$j</span> + <span class="s2">&quot; ist ein &quot;</span> + <span class="nv">$Tag</span>
</pre></div>
</div>
</div></blockquote>
</li>
<li><p class="first">Schreiben Sie eine Funktion, die ihnen die Größe des vorhandenen physikalischen und virtuellen RAMs ausgibt. Benutzen Sie zum Ermitteln der Größe die WMI-Klasse WIN32_OperatingSystem und deren Eigenschaften <strong>FreePhysicalMemory</strong> sowie <strong>FreeVirtualMemory</strong>. Als kleine &#8220;Zugabe&#8221; möchten Sie die Angabe der Größe in verschiedenen Formaten (Byte, KByte, MByte) ermöglichen.</p>
</li>
</ul>
<div class="highlight-sh"><div class="highlight"><pre><span class="k">function </span>get-memory <span class="o">([</span>string<span class="o">]</span><span class="nv">$units</span> <span class="o">=</span> <span class="s2">&quot;b&quot;</span><span class="o">)</span> <span class="o">{</span>
         <span class="nv">$comp</span> <span class="o">=</span> get-wmiobject Win32_OperatingSystem
         <span class="nv">$mem</span> <span class="o">=</span> <span class="nv">$comp</span>.FreePhysicalMemory + <span class="nv">$comp</span>.FreeVirtualMemory
         switch <span class="o">(</span><span class="nv">$units</span><span class="o">)</span> <span class="o">{</span>
                  <span class="s2">&quot;b&quot;</span> <span class="o">{</span><span class="nv">$mem</span><span class="o">}</span>
                  <span class="s2">&quot;k&quot;</span> <span class="o">{[</span>int<span class="o">](</span><span class="nv">$mem</span> / 1024<span class="o">)}</span>
                  <span class="s2">&quot;m&quot;</span> <span class="o">{[</span>int<span class="o">]((</span><span class="nv">$mem</span> * 100<span class="o">)</span> / 1048576<span class="o">)</span> / 100<span class="o">}</span>
         <span class="o">}</span>
<span class="o">}</span>
</pre></div>
</div>
<ul class="simple">
<li>Schreiben Sie eine Funktion zum Umbenennen eines Computernamens. Der Name soll entweder per Eingabeaufforderung übergeben werden können oder aus einer Textdatei gelesen werden, falls keine Eingabe vorgenommen wird. Kommentieren Sie die Funktion über die Powershell-typische Hilfesyntax. Prüfen Sie innerhalb der Funktion, ob Sie überhaupt die administrativen Rechte für die Umbenennung haben. Zum Abschluss des Vorgangs müssen Sie einen Reboot vornehmen.</li>
</ul>
<p>siehe: <a class="reference external" href="http://www.gangleri.net/2009/11/25/ChangingComputerNameAndWorkgroupWithPowerShell.aspx">http://www.gangleri.net/2009/11/25/ChangingComputerNameAndWorkgroupWithPowerShell.aspx</a></p>
<div class="highlight-sh"><div class="highlight"><pre><span class="k">function </span>Set-ComputerName
<span class="o">{</span>
        &lt;<span class="c">#</span>
        .SYNOPSIS
                Sets the name of the computer
        .DESCRIPTION
                Uses WMI to <span class="nb">set </span>the name of the computer, <span class="k">if </span>the name is successfully changed the user will be prompted to reboot and apply the changes
        .NOTES
                File Name: ComputerName.psm1
                Aurthor: Alan Bradley
                Requires: PowerShell 2.0
        .LINK
                http://www.gangleri.net
        .EXAMPLE
                Set-ComputerName -Name <span class="s2">&quot;{New name for computer}&quot;</span>
        .EXAMPLE
                Set-ComputerName <span class="s2">&quot;{New name for computer}&quot;</span>
        .EXAMPLE
                Set-ComputerName -Name <span class="s2">&quot;{New name for computer}&quot;</span> -AutoReboot
        .EXAMPLE
                Set-ComputerName <span class="s2">&quot;{New name for computer}&quot;</span> -AutoReboot
        .PARAMETER Name
                The new name that the computer will be known by
        <span class="c">#&gt;</span>
        param<span class="o">(</span>
                <span class="o">[</span>Parameter<span class="o">(</span><span class="nv">Position</span><span class="o">=</span>0, <span class="nv">Mandatory</span><span class="o">=</span><span class="nv">$true</span>,ParameterSetName<span class="o">=</span><span class="s2">&quot;Name&quot;</span><span class="o">)]</span>
                <span class="o">[</span>string<span class="o">]</span><span class="nv">$Name</span>,
                <span class="o">[</span>Switch<span class="o">]</span><span class="nv">$AutoReboot</span>
        <span class="o">)</span>
        process
        <span class="o">{</span>
                <span class="nv">$sysInfo</span> <span class="o">=</span> Get-WmiObject -Class Win32_ComputerSystem
                <span class="nv">$result</span> <span class="o">=</span> <span class="nv">$sysInfo</span>.Rename<span class="o">(</span><span class="nv">$Name</span><span class="o">)</span>

                switch<span class="o">(</span><span class="nv">$result</span>.ReturnValue<span class="o">)</span>
                <span class="o">{</span>
                        0
                        <span class="o">{</span>
                                Write-Host -ForegroundColor Green <span class="s2">&quot;Success&quot;</span>
                                <span class="k">if</span><span class="o">(</span><span class="nv">$AutoReboot</span> -eq <span class="nv">$false</span><span class="o">)</span>
                                <span class="o">{</span>
                                        <span class="nv">$yes</span> <span class="o">=</span> New-Object System.Management.Automation.Host.ChoiceDescription <span class="s2">&quot;&amp;Yes&quot;</span>,
                                                <span class="s2">&quot;Reboots the machine.&quot;</span>
                                        <span class="nv">$no</span> <span class="o">=</span> New-Object System.Management.Automation.Host.ChoiceDescription <span class="s2">&quot;&amp;No&quot;</span>,
                                                <span class="s2">&quot;Does not reboot the machine, you will manually have to reboot this machine for changes to take effect.&quot;</span>
                                        <span class="nv">$options</span> <span class="o">=</span> <span class="o">[</span>System.Management.Automation.Host.ChoiceDescription<span class="o">[]](</span><span class="nv">$yes</span>, <span class="nv">$no</span><span class="o">)</span>
                                        <span class="nv">$ShouldReboot</span> <span class="o">=</span> <span class="nv">$Host</span>.UI.PromptForChoice<span class="o">(</span><span class="s2">&quot;Reboot machine&quot;</span>,
                                                <span class="s2">&quot;You must reboot for change to take effect. Do you want to reboot now?&quot;</span>, <span class="nv">$options</span>, 0<span class="o">)</span>
                                <span class="o">}</span>
                                <span class="k">if</span><span class="o">(</span><span class="nv">$ShouldReboot</span> -eq 0 -or <span class="nv">$AutoReboot</span> -eq <span class="nv">$true</span><span class="o">)</span>
                                <span class="o">{</span>
                                        Restart-Computer
                                <span class="o">}</span>
                        <span class="o">}</span>
                        5 <span class="o">{</span> Write-Host -ForegroundColor Red <span class="s2">&quot;This cmdlet must be execute with administrative privileges&quot;</span> <span class="o">}</span>
                        default <span class="o">{</span> Write-Host -ForegroundColor Red <span class="s2">&quot;Error&quot;</span> <span class="o">}</span>
                <span class="o">}</span>
        <span class="o">}</span>
<span class="o">}</span>
</pre></div>
</div>
<div class="highlight-sh"><div class="highlight"><pre>&lt;<span class="c">#</span>
.SYNOPSIS
        Renames a computer
.DESCRIPTION
        This Script will rename a computer by passing the oldComputername and the new Computername. If no names are given, a textfile will be openend
.EXAMPLE
        .<span class="se">\S</span>et-Computername.ps1 <span class="s1">&#39;oldName&#39;</span> <span class="s1">&#39;newName&#39;</span>
.PARAMETER originalPCName
        Old name of the computer
.PARAMETER computerName
        new name of the computer
.NOTES
        FileName     : Download-File.ps1
        Author       : Steinam
        LastModified : 06 Apr 2011 9:39 AM PST
<span class="c">#Requires -Version 2.0</span>
<span class="c">#&gt;</span>
<span class="k">function </span>Set-ComputerName <span class="o">{</span>
        param<span class="o">([</span>switch<span class="o">]</span><span class="nv">$help</span>,
                  <span class="o">[</span>string<span class="o">]</span><span class="nv">$originalPCName</span><span class="o">=</span><span class="k">$(</span><span class="nb">read</span>-host <span class="s2">&quot;Please specify the current name of the computer&quot;</span><span class="k">)</span>,
                  <span class="o">[</span>string<span class="o">]</span><span class="nv">$computerName</span><span class="o">=</span><span class="k">$(</span><span class="nb">read</span>-host <span class="s2">&quot;Please specify the new name of the computer&quot;</span><span class="k">)</span><span class="o">)</span>

        <span class="nv">$usage</span> <span class="o">=</span> <span class="s2">&quot;set-ComputerName -originalPCname CurrentName -computername AnewName&quot;</span>
        <span class="k">if</span> <span class="o">(</span><span class="nv">$help</span><span class="o">)</span> <span class="o">{</span>Write-Host <span class="nv">$usage</span>;<span class="nb">break</span><span class="o">}</span>

        <span class="nv">$computer</span> <span class="o">=</span> Get-WmiObject Win32_ComputerSystem -OriginalPCname OriginalName -computername <span class="nv">$originalPCName</span>
        <span class="nv">$computer</span>.Rename<span class="o">(</span><span class="nv">$computerName</span><span class="o">)</span>
        <span class="o">}</span>


        <span class="c">#set-Computername</span>
        <span class="nv">$computer</span> <span class="o">=</span> Get-WmiObject Win32_ComputerSystem
        <span class="nv">$computer</span> | Format-List -Property N*
</pre></div>
</div>
<ul>
<li><dl class="first docutils">
<dt>Powershell kann mit Hilfe des CommandLets <strong>Get-Eventlog</strong> die Ereignisanzeige von lokalen und remote-Computern anzeigen und filtern. Schreiben Sie eine Funktion <em>getLogInfos</em>, die Ihnen folgende Auswahlmöglichkeiten zulässt:</dt>
<dd><ul class="first simple">
<li>Wahl des Computers</li>
<li>Wahl des jeweiligen EventLogs</li>
<li>Wahl des jeweiligen Eintrags, z.B. Error, Warnig, Information</li>
</ul>
<p>Informieren Sie sich mit Hilfe von get-help über die weiteren Möglichkeiten von Get-Eventlog</p>
<div class="last highlight-sh"><div class="highlight"><pre><span class="k">function </span>getLogInfo<span class="o">([</span>string<span class="o">]</span><span class="nv">$Computer</span><span class="o">=</span><span class="s2">&quot;.&quot;</span>, <span class="o">[</span>string<span class="o">]</span><span class="nv">$EventLog</span>, <span class="o">[</span>string<span class="o">]</span><span class="nv">$ErrorType</span><span class="o">)</span>
<span class="o">{</span>


<span class="o">}</span>
</pre></div>
</div>
</dd>
</dl>
</li>
<li><p class="first">Verändern von Attributen einer Datei</p>
<p>Sie haben als Administrator einige Dateien, auf die sie von Zeit zu Zeit zugreifen müssen. Um die Dateien vor versehentlichen Änderungen zu schützen, versehen Sie diese mit ReadOnly - Flag. Leider ist aber jetzt ein Bearbeiten dieser Dateien mühsam, weil Sie nun immer erst dieses Flag ändern müssen.</p>
<p>Schreiben Sie ein parametrisiertes Skript, welches alle Dateien eines Ordners ReadOnly bzw. Nicht-ReadOnly setzen kann. Schreiben Sie zusätzlich eine Funktion, die Ihnen alle Dateien mit ReadOnly-Flag auflistet.
Eine Hilfe-Funktion soll Sie bei einer falschen Eingabe unterstützen.</p>
<div class="highlight-sh"><div class="highlight"><pre> http://blogs.technet.com/b/heyscriptingguy/archive/2009/09/24/hey-scripting-guy-september-24-2009.aspx

 Param <span class="o">([</span>string<span class="o">]</span><span class="nv">$path</span><span class="o">=</span><span class="s1">&#39;c:\fso&#39;</span>,
<span class="o">[</span>switch<span class="o">]</span><span class="nv">$get</span>,
<span class="o">[</span>switch<span class="o">]</span><span class="nv">$clear</span>,
<span class="o">[</span>switch<span class="o">]</span><span class="nv">$set</span>,
<span class="o">[</span>switch<span class="o">]</span><span class="nv">$help</span>
 <span class="o">)</span><span class="c">#end param</span>

 <span class="c"># *** Functions here ***</span>

 Function Get-ReadOnlyFiles<span class="o">([</span>string<span class="o">]</span><span class="nv">$path</span><span class="o">)</span>
 <span class="o">{</span>
  <span class="s2">&quot;Getting readonly attribute on files in $path&quot;</span>
  Get-ChildItem -Path <span class="nv">$path</span> |
  Where-Object <span class="o">{</span> <span class="nv">$_</span>.attributes -match <span class="s1">&#39;readonly&#39;</span> <span class="o">}</span>
 <span class="o">}</span> <span class="c">#end get-Readonlyfiles</span>

 Function Clear-ReadOnlyFiles<span class="o">([</span>string<span class="o">]</span><span class="nv">$path</span><span class="o">)</span>
 <span class="o">{</span>
   <span class="s2">&quot;Clearing readonly attribute from files in $path&quot;</span>
  New-Variable -Name read_only -Value 1 -Option <span class="nb">readonly</span>
<span class="nb">  </span>Get-ChildItem -Path <span class="nv">$path</span> |
  Where-Object <span class="o">{</span> <span class="nv">$_</span>.attributes -match <span class="s1">&#39;readonly&#39;</span> <span class="o">}</span> |
  ForEach-Object <span class="o">{</span>
    <span class="nv">$_</span>.attributes <span class="o">=</span> <span class="nv">$_</span>.attributes -Bxor <span class="nv">$read_only</span> <span class="o">}</span>
 <span class="o">}</span><span class="c">#end Clear-ReadonlyFiles</span>

 Function Set-ReadOnlyFiles<span class="o">([</span>string<span class="o">]</span><span class="nv">$path</span><span class="o">)</span>
 <span class="o">{</span>
  <span class="s2">&quot;Setting readonly attribute on files in $path&quot;</span>
  New-Variable -Name read_only -Value 1 -Option <span class="nb">readonly</span>
<span class="nb">  </span>Get-ChildItem -Path <span class="nv">$path</span> |
  ForEach-Object <span class="o">{</span>
    <span class="nv">$_</span>.IsReadOnly <span class="o">=</span> <span class="nv">$True</span>
  <span class="o">}</span>
 <span class="o">}</span><span class="c">#end Set-ReadOnlyFiles</span>

 Function Get-HelpText
 <span class="o">{</span>
  <span class="nv">$helpText</span> <span class="o">=</span> @<span class="s2">&quot;</span>
<span class="s2">   WorkWithReadOnlyFiles.ps1 -path c:\fso -get</span>
<span class="s2">   Gets a list of all readonly files in c:\fso folder</span>

<span class="s2">   WorkWithReadOnlyFiles.ps1 -path c:\fso -set</span>
<span class="s2">   Sets all files in the c:\fso folder to readonly</span>

<span class="s2">   WorkWithReadOnlyFiles.ps1 -path c:\fso -clear</span>
<span class="s2">   Clears the readonly attribute from all files in c:\fso folder</span>
<span class="s2"> &quot;</span>@
  <span class="nv">$helpText</span>
 <span class="o">}</span><span class="c">#end Get-HelpText</span>


 <span class="c"># *** Entry Point to Script ***</span>
 <span class="c">#$clear = $true</span>
 <span class="k">if</span><span class="o">(</span><span class="nv">$help</span><span class="o">)</span> <span class="o">{</span> Get-HelpText ; <span class="nb">exit</span> <span class="o">}</span>
 <span class="k">if</span><span class="o">(</span>!<span class="nv">$path</span><span class="o">)</span> <span class="o">{</span> <span class="s2">&quot;A path is required.&quot;</span> ; Get-HelpText ; <span class="nb">exit</span> <span class="o">}</span>
 <span class="k">if</span><span class="o">(</span><span class="nv">$get</span><span class="o">)</span> <span class="o">{</span> Get-ReadOnlyFiles -path <span class="nv">$path</span> ; <span class="nb">exit</span> <span class="o">}</span>
 <span class="k">if</span><span class="o">(</span><span class="nv">$clear</span><span class="o">)</span> <span class="o">{</span> Clear-ReadOnlyFiles -path <span class="nv">$path</span> ; <span class="nb">exit</span> <span class="o">}</span>
 <span class="k">if</span><span class="o">(</span><span class="nv">$set</span><span class="o">)</span> <span class="o">{</span> Set-ReadonlyFiles -path <span class="nv">$path</span> ; <span class="nb">exit</span> <span class="o">}</span>
</pre></div>
</div>
</li>
<li><p class="first">Ein Außendienstmitarbeiter erhält ein neues Firmen-Laptop. Auf seinem alten Laptop sind viele WLAN-Profile gespeichert und Sie suchen einen Weg, um diese Profile auf den neuen Rechner ohne manuelles Neuanlegen zu übernehmen. Ein Kollege erzählt Ihnen, dass Sie mit Hilfe des Befehls netsh Profile sowie exportieren als auch importieren können.</p>
<ul>
<li><p class="first">Informieren Sie sich über die Möglichkeiten des Befehls netsh</p>
</li>
<li><p class="first">Schreiben Sie ein Skript, welches sowohl den Export als auch den Import der WLAN-Profile ermöglicht.</p>
<blockquote>
<div><div class="highlight-sh"><pre>function Export-WLAN {
&lt;#
.SYNOPSIS
        Exports all-user WLAN profiles
.DESCRIPTION
        Exports all-user WLAN profiles to Xml-files to the specified directory using netsh.exe
.PARAMETER XmlDirectory
        Directory to export Xml configuration-files to
.EXAMPLE
        Export-WLAN -XmlDirectory c:\temp\wlan
#&gt;

[CmdletBinding()]
        param (
                [parameter(Mandatory=$true)]
                [string]$XmlDirectory
                )

#Export all WLAN profiles to specified directory
$wlans = netsh wlan show profiles | Select-String -Pattern "Profil für alle Benutzer" | Foreach-Object {$_.ToString()}
$exportdata = $wlans | Foreach-Object {$_.Replace("    Profil für alle Benutzer : ",$null)}
$exportdata | ForEach-Object {netsh wlan export profile $_ $XmlDirectory}
}

function Import-WLAN {
&lt;#
.SYNOPSIS
        Imports all-user WLAN profiles based on Xml-files in the specified directory
.DESCRIPTION
        Imports all-user WLAN profiles based on Xml-files in the specified directory using netsh.exe
.PARAMETER XmlDirectory
        Directory to import Xml configuration-files from
.EXAMPLE
        Import-WLAN -XmlDirectory c:\temp\wlan
#&gt;

[CmdletBinding()]
        param (
                [parameter(Mandatory=$true)]
                [string]$XmlDirectory
                )

#Import all WLAN Xml-files from specified directory
Get-ChildItem $XmlDirectory | Where-Object {$_.extension -eq ".xml"} | ForEach-Object {netsh wlan add profile filename=($XmlDirectory+"\"+$_.name)}
}</pre>
</div>
</div></blockquote>
</li>
</ul>
</li>
<li><p class="first">Aufruf von Remote-Desktop-Sitzungen</p>
<blockquote>
<div><p>Erstellen Sie ein Skript, welches eine rdp-Verbindung unter Berücksichtigung verschiedener Optionen des mstsc-Befehls ermöglicht.</p>
<div class="highlight-sh"><div class="highlight"><pre><span class="c">########################################################################################################################</span>
    <span class="c"># NAME</span>
    <span class="c">#     Start-RDP</span>
    <span class="c">#</span>
    <span class="c"># SYNOPSIS</span>
    <span class="c">#     Opens a remote desktop connection to another computer.</span>
    <span class="c">#</span>
    <span class="c"># SYNTAX</span>
    <span class="c">#     Start-RDP [[-Server] &lt;string&gt;] [[-Width] &lt;int&gt;] [[-Height] &lt;int&gt;]</span>
    <span class="c">#     Start-RDP -Path &lt;string&gt; [[-Width] &lt;int&gt;] [[-Height] &lt;int&gt;]</span>
    <span class="c">#</span>
    <span class="c"># DETAILED DESCRIPTION</span>
    <span class="c">#     The Start-RDP cmdlet opens a new Remote Desktop connection using the Microsoft Terminal Services Client.</span>
    <span class="c">#     Connection settings can be specified by argument or read from a standard RDP file.</span>
    <span class="c">#</span>
    <span class="c"># PARAMETERS</span>
    <span class="c">#     -Server &lt;string&gt;</span>
    <span class="c">#         Specifies the name of the server to connect to.  May also include an IP address, domain, and/or port.</span>
    <span class="c">#</span>
    <span class="c">#         Required?                    false</span>
    <span class="c">#         Position?                    1</span>
    <span class="c">#         Default value</span>
    <span class="c">#         Accept pipeline input?       true</span>
    <span class="c">#         Accept wildcard characters?  false</span>
    <span class="c">#</span>
    <span class="c">#     -Width &lt;int&gt;</span>
    <span class="c">#         Specifies the desired width of the resolution for the connection (for non-full screen connections).</span>
    <span class="c">#</span>
    <span class="c">#         Required?                    false</span>
    <span class="c">#         Position?                    2</span>
    <span class="c">#         Default value</span>
    <span class="c">#         Accept pipeline input?       false</span>
    <span class="c">#         Accept wildcard characters?  false</span>
    <span class="c">#</span>
    <span class="c">#     -Height &lt;int&gt;</span>
    <span class="c">#         Specifies the desired height of the resolution for the connection (for non-full screen connections).</span>
    <span class="c">#</span>
    <span class="c">#         Required?                    false</span>
    <span class="c">#         Position?                    3</span>
    <span class="c">#         Default value</span>
    <span class="c">#         Accept pipeline input?       false</span>
    <span class="c">#         Accept wildcard characters?  false</span>
    <span class="c">#</span>
    <span class="c">#     -Path &lt;string&gt;</span>
    <span class="c">#         Specifies the path to an RDP file to connect with (resolution settings can be overridden using the</span>
    <span class="c">#         -Width and -Height parameters.</span>
    <span class="c">#</span>
    <span class="c">#         Required?                    false</span>
    <span class="c">#         Position?                    4</span>
    <span class="c">#         Default value</span>
    <span class="c">#         Accept pipeline input?       true</span>
    <span class="c">#         Accept wildcard characters?  false</span>
    <span class="c">#</span>
    <span class="c">#     -Console &lt;SwitchParameter&gt;</span>
    <span class="c">#         Connect to a Windows Server 2003 console session.</span>
    <span class="c">#</span>
    <span class="c">#         Required?                    false</span>
    <span class="c">#         Position?                    named</span>
    <span class="c">#         Default value                false</span>
    <span class="c">#         Accept pipeline input?       false</span>
    <span class="c">#         Accept wildcard characters?  false</span>
    <span class="c">#</span>
    <span class="c">#     -Admin &lt;SwitchParameter&gt;</span>
    <span class="c">#         Connect to a Windows Server 2008 administrator session.</span>
    <span class="c">#</span>
    <span class="c">#         Required?                    false</span>
    <span class="c">#         Position?                    named</span>
    <span class="c">#         Default value                false</span>
    <span class="c">#         Accept pipeline input?       false</span>
    <span class="c">#         Accept wildcard characters?  false</span>
    <span class="c">#</span>
    <span class="c">#     -Fullscreen &lt;SwitchParameter&gt;</span>
    <span class="c">#         Open connection in full screen mode.</span>
    <span class="c">#</span>
    <span class="c">#         Required?                    false</span>
    <span class="c">#         Position?                    named</span>
    <span class="c">#         Default value                false</span>
    <span class="c">#         Accept pipeline input?       false</span>
    <span class="c">#         Accept wildcard characters?  false</span>
    <span class="c">#</span>
    <span class="c">#     -Public &lt;SwitchParameter&gt;</span>
    <span class="c">#         Run Remote Desktop in public mode.</span>
    <span class="c">#</span>
    <span class="c">#         Required?                    false</span>
    <span class="c">#         Position?                    named</span>
    <span class="c">#         Default value                false</span>
    <span class="c">#         Accept pipeline input?       false</span>
    <span class="c">#         Accept wildcard characters?  false</span>
    <span class="c">#</span>
    <span class="c">#     -Span &lt;SwitchParameter&gt;</span>
    <span class="c">#         Span the Remote Desktop connection across multiple monitors.  Each monitor must have the same height</span>
    <span class="c">#         and be arranged vertically.</span>
    <span class="c">#</span>
    <span class="c">#         Required?                    false</span>
    <span class="c">#         Position?                    named</span>
    <span class="c">#         Default value                false</span>
    <span class="c">#         Accept pipeline input?       false</span>
    <span class="c">#         Accept wildcard characters?  false</span>
    <span class="c">#</span>
    <span class="c"># INPUT TYPE</span>
    <span class="c">#     String,System.IO.FileInfo</span>
    <span class="c">#</span>
    <span class="c"># RETURN TYPE</span>
    <span class="c">#</span>
    <span class="c">#</span>
    <span class="c"># NOTES</span>
    <span class="c">#</span>
    <span class="c">#     -------------------------- EXAMPLE 1 --------------------------</span>
    <span class="c">#</span>
    <span class="c">#     C:\PS&gt;Start-RDP</span>
    <span class="c">#</span>
    <span class="c">#</span>
    <span class="c">#     This command opens the Terminal Services Client connection dialog to specify a connection.</span>
    <span class="c">#</span>
    <span class="c">#</span>
    <span class="c">#     -------------------------- EXAMPLE 2 --------------------------</span>
    <span class="c">#</span>
    <span class="c">#     C:\PS&gt;Start-RDP -Server myserver -Width 1024 -Height 768</span>
    <span class="c">#</span>
    <span class="c">#</span>
    <span class="c">#     This command opens a new Remote Desktop connection to the server named &quot;myserver&quot; in a window with 1024x768 resolution.</span>
    <span class="c">#</span>
    <span class="c">#</span>
    <span class="c">#     -------------------------- EXAMPLE 3 --------------------------</span>
    <span class="c">#</span>
    <span class="c">#     C:\PS&gt;Start-RDP -Server myserver -Fullscreen</span>
    <span class="c">#</span>
    <span class="c">#</span>
    <span class="c">#     This command opens a new full screen Remote Desktop connection to the server named &quot;myserver&quot;.</span>
    <span class="c">#</span>
    <span class="c">#</span>
    <span class="c">#     -------------------------- EXAMPLE 4 --------------------------</span>
    <span class="c">#</span>
    <span class="c">#     C:\PS&gt;Start-RDP -Path C:\myserver.rdp</span>
    <span class="c">#</span>
    <span class="c">#</span>
    <span class="c">#     This command opens a new Remote Desktop connection using the specified RDP file.</span>
    <span class="c">#</span>
    <span class="c">#</span>

    <span class="c">#Function global:Start-RDP {</span>
            param<span class="o">(</span>
                    <span class="o">[</span>string<span class="o">]</span><span class="nv">$Server</span> <span class="o">=</span> <span class="s2">&quot;&quot;</span>,
                    <span class="o">[</span>int<span class="o">]</span><span class="nv">$Width</span> <span class="o">=</span> <span class="s2">&quot;&quot;</span>,
                    <span class="o">[</span>int<span class="o">]</span><span class="nv">$Height</span> <span class="o">=</span> <span class="s2">&quot;&quot;</span>,
                    <span class="o">[</span>string<span class="o">]</span><span class="nv">$Path</span> <span class="o">=</span> <span class="s2">&quot;&quot;</span>,
                    <span class="o">[</span>switch<span class="o">]</span><span class="nv">$Console</span>,
                    <span class="o">[</span>switch<span class="o">]</span><span class="nv">$Admin</span>,
                    <span class="o">[</span>switch<span class="o">]</span><span class="nv">$Fullscreen</span>,
                    <span class="o">[</span>switch<span class="o">]</span><span class="nv">$Public</span>,
                    <span class="o">[</span>switch<span class="o">]</span><span class="nv">$Span</span>
            <span class="o">)</span>

            begin <span class="o">{</span>
                    <span class="nv">$arguments</span> <span class="o">=</span> <span class="s2">&quot;&quot;</span>
                    <span class="nv">$dimensions</span> <span class="o">=</span> <span class="s2">&quot;&quot;</span>
                    <span class="nv">$processed</span> <span class="o">=</span> <span class="nv">$false</span>

                    <span class="k">if</span> <span class="o">(</span><span class="nv">$admin</span><span class="o">)</span> <span class="o">{</span>
                            <span class="nv">$arguments</span> +<span class="o">=</span> <span class="s2">&quot;/admin &quot;</span>
                    <span class="o">}</span> elseif <span class="o">(</span><span class="nv">$console</span><span class="o">)</span> <span class="o">{</span>
                            <span class="nv">$arguments</span> +<span class="o">=</span> <span class="s2">&quot;/console &quot;</span>
                    <span class="o">}</span>
                    <span class="k">if</span> <span class="o">(</span><span class="nv">$fullscreen</span><span class="o">)</span> <span class="o">{</span>
                            <span class="nv">$arguments</span> +<span class="o">=</span> <span class="s2">&quot;/f &quot;</span>
                    <span class="o">}</span>
                    <span class="k">if</span> <span class="o">(</span><span class="nv">$public</span><span class="o">)</span> <span class="o">{</span>
                            <span class="nv">$arguments</span> +<span class="o">=</span> <span class="s2">&quot;/public &quot;</span>
                    <span class="o">}</span>
                    <span class="k">if</span> <span class="o">(</span><span class="nv">$span</span><span class="o">)</span> <span class="o">{</span>
                            <span class="nv">$arguments</span> +<span class="o">=</span> <span class="s2">&quot;/span &quot;</span>
                    <span class="o">}</span>

                    <span class="k">if</span> <span class="o">(</span><span class="nv">$width</span> -and <span class="nv">$height</span><span class="o">)</span> <span class="o">{</span>
                            <span class="nv">$dimensions</span> <span class="o">=</span> <span class="s2">&quot;/w:$width /h:$height&quot;</span>
                    <span class="o">}</span>
            <span class="o">}</span>

            process <span class="o">{</span>
                    Function script:executePath<span class="o">([</span>string<span class="o">]</span><span class="nv">$path</span><span class="o">)</span> <span class="o">{</span>
                            Invoke-Expression <span class="s2">&quot;mstsc.exe &#39;$path&#39; $dimensions $arguments&quot;</span>
                    <span class="o">}</span>
                    Function script:executeArguments<span class="o">([</span>string<span class="o">]</span><span class="nv">$Server</span><span class="o">)</span> <span class="o">{</span>
                            Invoke-Expression <span class="s2">&quot;mstsc.exe /v:$server $dimensions $arguments&quot;</span>
                    <span class="o">}</span>

                    <span class="k">if</span> <span class="o">(</span><span class="nv">$_</span><span class="o">)</span> <span class="o">{</span>
                            <span class="k">if</span> <span class="o">(</span><span class="nv">$_</span> -is <span class="o">[</span>string<span class="o">])</span> <span class="o">{</span>
                                    <span class="k">if</span> <span class="o">(</span><span class="nv">$_</span> -imatch <span class="s1">&#39;\.rdp$&#39;</span><span class="o">)</span> <span class="o">{</span>
                                            <span class="k">if</span> <span class="o">(</span>Test-Path <span class="nv">$_</span><span class="o">)</span> <span class="o">{</span>
                                                    executePath <span class="nv">$_</span>
                                                    <span class="nv">$processed</span> <span class="o">=</span> <span class="nv">$true</span>
                                            <span class="o">}</span> <span class="k">else</span> <span class="o">{</span>
                                                    throw <span class="s2">&quot;Path does not exist.&quot;</span>
                                            <span class="o">}</span>
                                    <span class="o">}</span> <span class="k">else</span> <span class="o">{</span>
                                            executeArguments <span class="nv">$_</span>
                                            <span class="nv">$processed</span> <span class="o">=</span> <span class="nv">$true</span>
                                    <span class="o">}</span>
                            <span class="o">}</span> elseif <span class="o">(</span><span class="nv">$_</span> -is <span class="o">[</span>System.IO.FileInfo<span class="o">])</span> <span class="o">{</span>
                                    <span class="k">if</span> <span class="o">(</span>Test-Path <span class="nv">$_</span>.FullName<span class="o">)</span> <span class="o">{</span>
                                            executePath <span class="nv">$_</span>.FullName
                                            <span class="nv">$processed</span> <span class="o">=</span> <span class="nv">$true</span>
                                    <span class="o">}</span> <span class="k">else</span> <span class="o">{</span>
                                            throw <span class="s2">&quot;Path does not exist.&quot;</span>
                                    <span class="o">}</span>
                            <span class="o">}</span> elseif <span class="o">(</span><span class="nv">$_</span>.Path<span class="o">)</span> <span class="o">{</span>
                                    <span class="k">if</span> <span class="o">(</span>Test-Path <span class="nv">$_</span>.Path<span class="o">)</span> <span class="o">{</span>
                                            executePath <span class="nv">$_</span>.Path
                                            <span class="nv">$processed</span> <span class="o">=</span> <span class="nv">$true</span>
                                    <span class="o">}</span> <span class="k">else</span> <span class="o">{</span>
                                            throw <span class="s2">&quot;Path does not exist.&quot;</span>
                                    <span class="o">}</span>
                            <span class="o">}</span> elseif <span class="o">(</span><span class="nv">$_</span>.DnsName<span class="o">)</span> <span class="o">{</span>
                                    executeArguments <span class="nv">$_</span>.DnsName
                                    <span class="nv">$processed</span> <span class="o">=</span> <span class="nv">$true</span>
                            <span class="o">}</span> elseif <span class="o">(</span><span class="nv">$_</span>.Server<span class="o">)</span> <span class="o">{</span>
                                    executeArguments <span class="nv">$_</span>.Server
                                    <span class="nv">$processed</span> <span class="o">=</span> <span class="nv">$true</span>
                            <span class="o">}</span> elseif <span class="o">(</span><span class="nv">$_</span>.ServerName<span class="o">)</span> <span class="o">{</span>
                                    executeArguments <span class="nv">$_</span>.ServerName
                                    <span class="nv">$processed</span> <span class="o">=</span> <span class="nv">$true</span>
                            <span class="o">}</span> elseif <span class="o">(</span><span class="nv">$_</span>.Name<span class="o">)</span> <span class="o">{</span>
                                    executeArguments <span class="nv">$_</span>.Name
                                    <span class="nv">$processed</span> <span class="o">=</span> <span class="nv">$true</span>
                            <span class="o">}</span>
                    <span class="o">}</span>
            <span class="o">}</span>

            end <span class="o">{</span>
                    <span class="k">if</span> <span class="o">(</span><span class="nv">$path</span><span class="o">)</span> <span class="o">{</span>
                            <span class="k">if</span> <span class="o">(</span>Test-Path <span class="nv">$path</span><span class="o">)</span> <span class="o">{</span>
                                    Invoke-Expression <span class="s2">&quot;mstsc.exe &#39;$path&#39; $dimensions $arguments&quot;</span>

                            <span class="o">}</span> <span class="k">else</span> <span class="o">{</span>
                                    throw <span class="s2">&quot;Path does not exist.&quot;</span>
                            <span class="o">}</span>
                    <span class="o">}</span> elseif <span class="o">(</span><span class="nv">$server</span><span class="o">)</span> <span class="o">{</span>
                            Invoke-Expression <span class="s2">&quot;mstsc.exe /v:$server $dimensions $arguments&quot;</span>
                    <span class="o">}</span> elseif <span class="o">(</span>-not <span class="nv">$processed</span><span class="o">)</span> <span class="o">{</span>
                            Invoke-Expression <span class="s2">&quot;mstsc.exe $dimensions $arguments&quot;</span>
                    <span class="o">}</span>
            <span class="o">}</span>
    <span class="c">#}</span>
</pre></div>
</div>
</div></blockquote>
</li>
<li><p class="first">Setzen Sie mit Hilfe der PS die IP-Adresse eines Rechners. Geben Sie auch die Daten für Gateway, SN-Mask, NIC, DNS-Server</p>
<blockquote>
<div><div class="highlight-sh"><div class="highlight"><pre>        <span class="c"># corrected version - $mask variable corrected to match in both places</span>
        <span class="k">function </span>Set-IPAddress <span class="o">{</span>
    param<span class="o">(</span>  <span class="o">[</span>string<span class="o">]</span><span class="nv">$networkinterface</span> <span class="o">=</span><span class="k">$(</span><span class="nb">read</span>-host <span class="s2">&quot;Enter the name</span>
<span class="s2">                    of the NIC (ie Local Area Connection)&quot;</span><span class="k">)</span>,
                    <span class="o">[</span>string<span class="o">]</span><span class="nv">$ip</span> <span class="o">=</span> <span class="k">$(</span><span class="nb">read</span>-host <span class="s2">&quot;Enter an IP Address (ie 10.10.10.10)&quot;</span><span class="k">)</span>,
                    <span class="o">[</span>string<span class="o">]</span><span class="nv">$mask</span> <span class="o">=</span> <span class="k">$(</span><span class="nb">read</span>-host <span class="s2">&quot;Enter the subnet mask (ie 255.255.255.0)&quot;</span><span class="k">)</span>,
                    <span class="o">[</span>string<span class="o">]</span><span class="nv">$gateway</span> <span class="o">=</span> <span class="k">$(</span><span class="nb">read</span>-host <span class="s2">&quot;Enter the current name of the NIC you want to rename&quot;</span><span class="k">)</span>,
                    <span class="o">[</span>string<span class="o">]</span><span class="nv">$dns1</span> <span class="o">=</span> <span class="k">$(</span><span class="nb">read</span>-host <span class="s2">&quot;Enter the first DNS Server (ie 10.2.0.28)&quot;</span><span class="k">)</span>,
                    <span class="o">[</span>string<span class="o">]</span><span class="nv">$dns2</span>,
                    <span class="o">[</span>string<span class="o">]</span><span class="nv">$registerDns</span> <span class="o">=</span> <span class="s2">&quot;TRUE&quot;</span>
                    <span class="o">)</span>

                    <span class="nv">$dns</span> <span class="o">=</span> <span class="nv">$dns1</span>
    <span class="k">if</span><span class="o">(</span><span class="nv">$dns2</span><span class="o">){</span><span class="nv">$dns</span> <span class="o">=</span><span class="s2">&quot;$dns1,$dns2&quot;</span><span class="o">}</span>
    <span class="nv">$index</span> <span class="o">=</span> <span class="o">(</span>gwmi Win32_NetworkAdapter | where <span class="o">{</span><span class="nv">$_</span>.netconnectionid -eq <span class="nv">$networkinterface</span><span class="o">})</span>.InterfaceIndex
    <span class="nv">$NetInterface</span> <span class="o">=</span> Get-WmiObject Win32_NetworkAdapterConfiguration | where <span class="o">{</span><span class="nv">$_</span>.InterfaceIndex -eq <span class="nv">$index</span><span class="o">}</span>
    <span class="nv">$NetInterface</span>.EnableStatic<span class="o">(</span><span class="nv">$ip</span>, <span class="nv">$mask</span><span class="o">)</span>
    <span class="nv">$NetInterface</span>.SetGateways<span class="o">(</span><span class="nv">$gateway</span><span class="o">)</span>
    <span class="nv">$NetInterface</span>.SetDNSServerSearchOrder<span class="o">(</span><span class="nv">$dns</span><span class="o">)</span>
    <span class="nv">$NetInterface</span>.SetDynamicDNSRegistration<span class="o">(</span><span class="nv">$registerDns</span><span class="o">)</span>

<span class="o">}</span>
</pre></div>
</div>
</div></blockquote>
</li>
<li><p class="first">Mit Hilfe des Befehls <strong>net view \UNC-Name</strong> können Sie die Freigaben eines Rechners ermitteln. Schreiben Sie eine Funktion, die als Parameter einen UNC-Namen und einen speziellen Freigabetyp erwartet. Die Funktion soll dann die gefundenen Freigaben zurückgeben.</p>
</li>
</ul>
<div class="highlight-sh"><div class="highlight"><pre><span class="k">function </span>Get-ShareNames<span class="o">([</span>string<span class="o">]</span><span class="nv">$server</span>, <span class="o">[</span>string<span class="o">]</span><span class="nv">$ShareType</span><span class="o">)</span>
<span class="o">{</span>
&lt;<span class="c">#</span>
        .SYNOPSIS
                Get-Sharenames

        .DESCRIPTION
                Gets all printers from specified server
                using intern the net view <span class="nb">command</span>

        .Parameter Server
                Specified Servername who holds the shares

        .Parameter ShareType
                Specify <span class="nb">type </span>of shares, e.g. <span class="s2">&quot;Drucker&quot;</span> or <span class="s2">&quot;Platte&quot;</span>


<span class="c">#&gt;</span>

        <span class="nv">$result</span> <span class="o">=</span> net view <span class="nv">$server</span> | Select-String <span class="nv">$ShareType</span>
        <span class="c">#$result = net view &quot;\\win-srv11&quot; | Select-String &quot;Drucker&quot;</span>

        <span class="nv">$ausgabe</span> <span class="o">=</span> @<span class="o">()</span>


        foreach<span class="o">(</span><span class="nv">$element</span> in <span class="nv">$result</span><span class="o">)</span>
        <span class="o">{</span>
                 <span class="c">#nächste Zeile ist notwendig, da $element kein string</span>
                 <span class="c">#sondern ein matchInfo-Objekt ist</span>
                 <span class="c">#Wahrscheinlich wegen dem Select-String-ComandLet</span>
                 <span class="nv">$zeile</span> <span class="o">=</span> <span class="nv">$element</span>.toString<span class="o">()</span>
                 <span class="nv">$name</span> <span class="o">=</span> <span class="nv">$zeile</span>.split<span class="o">(</span><span class="s2">&quot;Drucker&quot;</span><span class="o">)</span>
                 <span class="nv">$ergebnis</span> <span class="o">=</span> <span class="nv">$name</span><span class="o">[</span>0<span class="o">]</span>.TrimEnd<span class="o">()</span>
                 <span class="nv">$ausgabe</span> +<span class="o">=</span> <span class="nv">$ergebnis</span>
        <span class="o">}</span>


        <span class="k">return</span> <span class="nv">$ausgabe</span>

<span class="o">}</span>


Get-Sharenames <span class="s2">&quot;\\win-srv11&quot;</span> <span class="s2">&quot;Drucker&quot;</span>


Get-ShareNames
</pre></div>
</div>
</div>
<div class="section" id="pipeline">
<span id="loesung-powershell-pipeline"></span><h2>14.10. Pipeline<a class="headerlink" href="#pipeline" title="Permalink zu dieser Überschrift">¶</a></h2>
<ul id="loesung-pipeline">
<li><p class="first">Virtual Memory</p>
<div class="highlight-sh"><div class="highlight"><pre>Get-Process | Sort-Object WorkingSet | Select-Object -First 5
</pre></div>
</div>
</li>
<li><p class="first">Largest File</p>
<div class="highlight-sh"><div class="highlight"><pre>Get-ChildItem -Recurse | Where-Object <span class="o">{</span> <span class="nv">$_</span>.Length -gt 10MB <span class="o">}</span> |
Sort-Object Length -Descending | Select-Object -First 10
</pre></div>
</div>
</li>
<li><p class="first">Foreach: Foreach-Objekt besrbeitet die Elemente der Pipeline sofort</p>
<div class="highlight-sh"><div class="highlight"><pre><span class="c"># ForEach-Object lists each element in a pipeline:</span>
Dir C:<span class="se">\ </span>-recurse | ForEach-Object <span class="o">{</span> <span class="nv">$_</span>.name <span class="o">}</span>
</pre></div>
</div>
</li>
<li><p class="first">Alias:</p>
<blockquote>
<div><div class="highlight-sh"><div class="highlight"><pre>Get-Alias | where-object <span class="o">{</span> <span class="nv">$_</span>.definition -eq <span class="s2">&quot;get-process&quot;</span> <span class="o">}</span>
</pre></div>
</div>
</div></blockquote>
</li>
<li><p class="first">20 MByte-Prozesse</p>
<blockquote>
<div><div class="highlight-sh"><div class="highlight"><pre>get-process | where-object <span class="o">{</span><span class="nv">$_</span>.WorkingSet64 -gt 20MB <span class="o">}</span>
ps | ? <span class="o">{</span><span class="nv">$_</span>.ws -gt 20MB <span class="o">}</span>
</pre></div>
</div>
</div></blockquote>
</li>
<li><p class="first">Sortiert nach GrÃƒÂ¶ÃƒÅ¸ee</p>
<blockquote>
<div><div class="highlight-sh"><div class="highlight"><pre>ps | ? <span class="o">{</span><span class="nv">$_</span>.ws -gt 20MB <span class="o">}</span> | sort ws
</pre></div>
</div>
</div></blockquote>
</li>
<li><p class="first">Ausgabe Produktversion</p>
<blockquote>
<div><div class="highlight-sh"><div class="highlight"><pre>ps | <span class="k">select </span>company, name, product, productversion
</pre></div>
</div>
</div></blockquote>
</li>
<li><p class="first">Dateien</p>
<blockquote>
<div><div class="highlight-sh"><div class="highlight"><pre>Get-Childitem h:<span class="se">\d</span>aten -filter *.doc -r
| Where-Object <span class="o">{</span> <span class="nv">$_</span>.Length -gt 40000 <span class="o">}</span>
| Select-Object Name, Length
| Sort-Object Length
| <span class="nb">export</span>-csv p:<span class="se">\G</span>rosseWordDateien.csv -notype
</pre></div>
</div>
</div></blockquote>
</li>
<li><p class="first">Geben Sie alle Unterordner bis zu einer bestimmten Tiefe aus, ausgehend vom jeweils aktuellen Ordner</p>
</li>
</ul>
<blockquote>
<div><p>$Cred = New-Object System.Management.Automation.PSCredential &#8220;Administrator&#8221;, $Pw</p>
<div class="highlight-sh"><pre>gci -recurse | ? {$_.PSisContainer -eq $true} | where{
       if(($_.fullname.split("\")).count -le 4)
       {
               write-host $_.fullname
       }
}</pre>
</div>
</div></blockquote>
<ul>
<li><p class="first">Sie wollen prÃƒÂ¼fen, ob auf bestimmten Rechnern innerhalb ihres LANs Software bestimmter Hersteller vorhanden ist. Schreiben Sie ein entsprechendes Skript</p>
<div class="highlight-sh"><div class="highlight"><pre><span class="c">########################################</span>
<span class="c"># The PowerShell script inventories the installed software</span>
<span class="c"># of a producer on n computer systems</span>
<span class="c"># (C) Dr. Holger Schwichtenberg</span>
<span class="c">########################################</span>
<span class="nv">$Producer</span> <span class="o">=</span> <span class="s2">&quot;*Microsoft*&quot;</span>
<span class="nv">$EntryFilename</span> <span class="o">=</span> <span class="s2">&quot;computernames.txt&quot;</span>
<span class="nv">$OutputFilename</span> <span class="o">=</span> <span class="s2">&quot;Softwareinventory.csv&quot;</span>
<span class="nv">$pw</span> <span class="o">=</span> Read-Host -AsSecureString




<span class="c"># Import of computer names</span>
<span class="nv">$Computernames</span> <span class="o">=</span> Get-Content <span class="s2">&quot;computernames.txt&quot;</span>
<span class="nv">$Computernames</span> | foreach <span class="o">{</span>
       <span class="k">if</span> <span class="o">(</span>Ping<span class="o">(</span><span class="nv">$_</span><span class="o">))</span>
       <span class="o">{</span>
               Write-Host <span class="s2">&quot;Inventorize software for computer $_ ...&quot;</span>
               <span class="c"># Fetching of installed MSI packages on all computers</span>

               <span class="nv">$Cred</span> <span class="o">=</span> New-Object System.Management.Automation.PSCredential <span class="nv">$_</span><span class="se">\A</span>dministrator, <span class="nv">$Pw</span>

               <span class="nv">$Software</span> <span class="o">=</span> foreach <span class="o">{</span> get-wmiobject win32_product -computername <span class="nv">$_</span> -credential <span class="nv">$Cred</span> <span class="o">}</span>
               | where <span class="o">{</span> <span class="nv">$_</span>.vendor -like <span class="nv">$Producer</span> <span class="o">}</span>
               <span class="c"># Export in CSV</span>
               <span class="nv">$Software</span> | <span class="nb">export</span>-csv <span class="nv">$OutputFileName</span> -notypeinformation
       <span class="o">}</span>
       <span class="k">else</span>
       <span class="o">{</span>
               Write-Error <span class="s2">&quot;Computer not accessible!&quot;</span>
       <span class="o">}</span>
<span class="o">}</span>
<span class="c"># Execute Ping</span>
<span class="k">function </span>Ping
<span class="o">{</span>
       <span class="nv">$status</span> <span class="o">=</span> Get-WmiObject Win32_PingStatus -Ã¯Â¬ï¿½lter <span class="s2">&quot;Address=&#39;$args[0]&#39;&quot;</span> |
       <span class="k">select </span>StatusCode
       <span class="k">return</span> <span class="nv">$status</span>.Statuscode -eq 0
<span class="o">}</span>
</pre></div>
</div>
</li>
</ul>
<p>Das Skript ist in mehrere Teile gegliedert:</p>
<ol class="arabic">
<li><p class="first">Function Ping</p>
<div class="highlight-sh"><div class="highlight"><pre><span class="c"># Execute Ping</span>
<span class="k">function </span>Ping
<span class="o">{</span>
       <span class="nv">$status</span> <span class="o">=</span> Get-WmiObject Win32_PingStatus -Ã¯Â¬ï¿½lter <span class="s2">&quot;Address=&#39;$args[0]&#39;&quot;</span> |
       <span class="k">select </span>StatusCode
       <span class="k">return</span> <span class="nv">$status</span>.Statuscode -eq 0
<span class="o">}</span>
</pre></div>
</div>
<p>ErhÃƒÂ¤lt als Parameter einen Computernamen, ermittelt den Statuscode und gibt true zurÃƒÂ¼ck, wenn der Computer erreichbar ist</p>
</li>
<li><p class="first">Einstieg in das Skript</p>
<blockquote>
<div><div class="highlight-sh"><div class="highlight"><pre><span class="nv">$Producer</span> <span class="o">=</span> <span class="s2">&quot;*Microsoft*&quot;</span>
<span class="nv">$Entry</span>Ã¯Â¬ï¿½lename <span class="o">=</span> <span class="s2">&quot;computernames.txt&quot;</span>
<span class="nv">$Output</span>Ã¯Â¬ï¿½lename <span class="o">=</span> <span class="s2">&quot;Softwareinventory.csv&quot;</span>
<span class="c"># Import of computer names</span>
<span class="nv">$Computernames</span> <span class="o">=</span> Get-Content <span class="s2">&quot;computernames.txt&quot;</span>
</pre></div>
</div>
<p>Definition von einigen Variablen
Lesen der zu ÃƒÂ¼berprÃƒÂ¼fenden Computernamen aus einer Textdatei</p>
</div></blockquote>
</li>
<li><p class="first">Informationsbeschaffung</p>
<div class="highlight-sh"><div class="highlight"><pre><span class="nv">$Computernames</span> | foreach <span class="o">{</span>
  <span class="k">if</span> <span class="o">(</span>Ping<span class="o">(</span><span class="nv">$_</span><span class="o">))</span>
  <span class="o">{</span>
        Write-Host <span class="s2">&quot;Inventorize software for computer $_ ...&quot;</span>
        <span class="c"># Fetching of installed MSI packages on all computers</span>
        <span class="nv">$Software</span> <span class="o">=</span> foreach <span class="o">{</span> get-wmiobject win32_product -computername <span class="nv">$_</span> <span class="o">}</span>
        | where <span class="o">{</span> <span class="nv">$_</span>.vendor -like <span class="nv">$Producer</span> <span class="o">}</span>
        <span class="c"># Export in CSV</span>
        <span class="nv">$Software</span> | <span class="nb">export</span>-csv <span class="s2">&quot;Softwareinventar.csv&quot;</span> -notypeinformation
  <span class="o">}</span>
  <span class="k">else</span>
  <span class="o">{</span>
        Write-Error <span class="s2">&quot;Computer not accessible!&quot;</span>
  <span class="o">}</span>
<span class="o">}</span>
</pre></div>
</div>
<p>ÃƒÅ“ber eine Pipeline werden die Computernamen nacheinander aufgerufen
Bei erfolgreichem Ping wird per WMI der Remote-Computer auf das
Vorhandensein des Herstellernamens abgefragt. Bei Vorhandensein wird es als
Ergebnis in eine CSV-Datei geschrieben.</p>
</li>
</ol>
<ul>
<li><p class="first">Erweitern Sie obiges Skript um die Abfrage nach bestimmten Softwarenamen bzw.
VersionsstÃƒÂ¤nden.</p>
<div class="highlight-sh"><div class="highlight"><pre><span class="k">function </span>Get-IsInstall<span class="o">(</span><span class="nv">$Application</span>, <span class="nv">$Computer</span>, <span class="nv">$Version</span><span class="o">)</span>
<span class="o">{</span>
       <span class="nv">$a</span> <span class="o">=</span> <span class="o">(</span>Get-WmiObject -Class Win32_Product -Filter
       <span class="s2">&quot;Name=&#39;$Application&#39; and Version=&#39;$Version&#39;&quot;</span> -computername
       <span class="nv">$Computer</span><span class="o">)</span>
       <span class="k">return</span> <span class="o">(</span><span class="nv">$a</span> -ne <span class="nv">$null</span><span class="o">)</span>
<span class="o">}</span>
<span class="nv">$e</span> <span class="o">=</span> Get-IsInstall <span class="s2">&quot;QuickTime&quot;</span> <span class="s2">&quot;E01&quot;</span> <span class="s2">&quot;7.2.0.240&quot;</span>
<span class="k">if</span> <span class="o">(</span><span class="nv">$e</span><span class="o">)</span>
<span class="o">{</span>
       <span class="s2">&quot;Software is installed!&quot;</span>
<span class="o">}</span>
<span class="k">else</span>
<span class="o">{</span>
       <span class="s2">&quot;Software is not installed!&quot;</span>
<span class="o">}</span>
</pre></div>
</div>
</li>
</ul>
<ul class="simple" id="index-0">
<li>I needed to merge 365 CSV files that represent daily weather data sets into one CSV file that contains all the data accumulated during one year. Each of the daily CSV files had a header row. The yearly file should only have one. Filtering out rows is a perfect application of the PowerShell filter functions. Übersetzen Sie den anglischen Text sinngemäß¸ und schreiben Sie ein entsprechendes Skript.</li>
</ul>
<p>This is where PowerShell&#8217;s pipeline programming is shining. It only takes a Filter function definition, inserting it into an pre-existing pipeline and you are done.</p>
<p>Here is the sample script:</p>
<p><a class="reference external" href="http://www.tellingmachine.com/post/Using-PowerShell-Filter-functions-to-filter-out-header-rows-CSV-file-merges.aspx">http://www.tellingmachine.com/post/Using-PowerShell-Filter-functions-to-filter-out-header-rows-CSV-file-merges.aspx</a></p>
<div class="highlight-sh"><div class="highlight"><pre><span class="c">#Filter Function filters out records that start with &quot;Time&quot;</span>
Filter Filter-Header
<span class="o">{</span>
        <span class="k">if</span><span class="o">(</span><span class="nv">$_</span> -match <span class="s2">&quot;^Time&quot;</span><span class="o">)</span>
        <span class="o">{</span>
                <span class="nv">$_</span> | out-null
        <span class="o">}</span>
        <span class="k">else</span>
        <span class="o">{</span>
                <span class="nv">$_</span>
        <span class="o">}</span>
<span class="o">}</span>

<span class="nb">cd</span> <span class="s2">&quot;C:\WeatherStation\FilterTest&quot;</span>

<span class="c">#Initializing yearly file</span>
<span class="nv">$YearlyFileName</span> <span class="o">=</span> <span class="s2">&quot;Weather2009.csv&quot;</span>
<span class="c">#Set the header only once in the yearly file</span>
Set-Content -Path <span class="nv">$YearlyFileName</span> -value <span class="s2">&quot;Time,TemperatureF,DewpointF,PressureIn,WindDirectionDegrees,WindSpeedMPH,WindSpeedGustMPH,Humidity,HourlyPrecipIn&quot;</span> -force -encoding <span class="s2">&quot;UTF8&quot;</span>

<span class="c">#Open all csv files get-content and add it to the yearly file</span>
<span class="nv">$Files</span> <span class="o">=</span> dir -Path <span class="s2">&quot;C:\Users\Klaus\Desktop\TO PLANET\WeatherStation\FilterTest&quot;</span> -Filter <span class="s2">&quot;*.csv&quot;</span>

<span class="c">#Filter in Action</span>
<span class="nv">$Files</span> | ForEach-Object <span class="sb">`</span>
<span class="o">{</span>
        Get-Content -Path <span class="nv">$_</span>.Name -Encoding <span class="s2">&quot;UTF8&quot;</span> | Filter-Header | Add-Content -path <span class="nv">$YearlyFileName</span> -Encoding <span class="s2">&quot;UTF8&quot;</span>
<span class="o">}</span>
</pre></div>
</div>
<ul>
<li><p class="first">Suchen Sie die letzten 2000 Systemereignisse des Eventlogs, die vom Typ &#8220;Error&#8221; sind</p>
<blockquote>
<div><div class="highlight-sh"><div class="highlight"><pre><span class="c"># PowerShell script to find Error messages in the System eventlog.</span>
Get-EventLog system -newest 2000 | where <span class="o">{</span><span class="nv">$_</span>.entryType -match <span class="s2">&quot;Error&quot;</span><span class="o">}</span>
</pre></div>
</div>
</div></blockquote>
</li>
<li><p class="first">Sie schreiben ein Powershell-Skript, welches eine eigene Ereignisquelle im Eventlog schafft und darunter seine Ereignisse schreibt.</p>
<div class="highlight-sh"><div class="highlight"><pre><span class="c">## Step 1 - PowerShell Script to create eventlog source by David Stone</span>
Clear-Host
<span class="k">if</span> <span class="o">(</span>!<span class="o">(</span><span class="nb">test</span>-path <span class="sb">`</span> HKLM:<span class="se">\S</span>YSTEM<span class="se">\C</span>urrentControlSet<span class="se">\S</span>ervices<span class="se">\E</span>ventlog<span class="se">\A</span>pplication<span class="se">\S</span>ampleApp <span class="o">))</span> <span class="sb">`</span>
<span class="o">{</span>new-eventlog -logname Application -source SampleApp <span class="sb">`</span>
-ErrorAction SilentlyContinue<span class="o">}</span>

<span class="c">## Step 2 - Create a &#39;Job start&#39; entry in your event log</span>
<span class="nv">$startTime</span> <span class="o">=</span> Get-date
<span class="nv">$startLog</span> <span class="o">=</span> <span class="s1">&#39;Sample job started at &#39;</span> +<span class="nv">$startTime</span>+ <span class="s1">&#39; local time&#39;</span>
write-eventlog -LogName Application -Message <span class="nv">$startLog</span> -Source SampleApp <span class="sb">`</span>
-id 1 -entrytype Information -Category 0

<span class="c">## Step 3 - A production script would have a payload here.</span>

<span class="c">## Step 4 - Write errors during processing (typically part of a if statement)</span>
write-eventLog -LogName Application -Message <span class="s1">&#39;Message content&#39;</span> <span class="sb">`</span>
-Source SampleApp -id 100 -entrytype Warning -category 0

<span class="c">## Step 5 - Write end of process entry in your event log</span>
<span class="nv">$endTime</span> <span class="o">=</span> Get-date
<span class="nv">$endLog</span> <span class="o">=</span> <span class="s1">&#39;Sample job ended at &#39;</span> +<span class="nv">$endTime</span>+ <span class="s1">&#39; local time&#39;</span>
write-eventlog -LogName Application -Message <span class="nv">$endLog</span> -Source SampleApp <span class="sb">`</span>
-id 9 -entrytype Information -category 0
</pre></div>
</div>
</li>
<li><p class="first">Schreiben Sie die Einträge der obigen Quelle in eine CSV-Datei. Es soll nur die jeweils letzte Stunde eingetragen werden</p>
<blockquote>
<div><div class="highlight-sh"><div class="highlight"><pre><span class="c">## Step 6 - Write the entries to a csv file</span>
Clear-Host
<span class="nv">$logtime</span><span class="o">=[</span>DateTime<span class="o">]</span>::Now.AddHours<span class="o">(</span>-1<span class="o">)</span>
<span class="nv">$CSVPath</span> <span class="o">=</span> <span class="s2">&quot;C:\SSource.csv&quot;</span>
Get-EventLog -LogName application -Source <span class="s1">&#39;SampleApp&#39;</span> <span class="sb">`</span>
-EntryType warning -After <span class="nv">$logtime</span> <span class="sb">`</span>
| <span class="k">select </span>eventid, machinename, entrytype, <span class="nb">source</span>, message, timegenerated <span class="sb">`</span>
| Export-Csv <span class="nv">$CSVPath</span> -NoTypeInformation
</pre></div>
</div>
</div></blockquote>
</li>
<li><p class="first">A Real-life Example of Select-String</p>
<p>My practical problem was that I wanted to search for instances a particular string in a directory with hundreds of file.  If a file had duplicate entries, then I needed to know.</p>
<p>The PowerShell problem is how to create a stream of the files, then compare each file with my string value.  In the output I needed to know the filename.</p>
<p>To solve the problem, I employed four main commands, which you can see in the following script:</p>
<div class="highlight-sh"><div class="highlight"><pre>    Get-ChildItem - recurse
    foreach <span class="o">(</span>loop<span class="o">)</span> <span class="o">{</span>what to <span class="k">do </span>block<span class="o">}</span>
<span class="k">if</span> <span class="o">(</span><span class="nb">test</span><span class="o">)</span> <span class="o">{</span>output block<span class="o">}</span>
<span class="k">select</span>-String -pattern to match my string value.
</pre></div>
</div>
<div class="highlight-sh"><div class="highlight"><pre><span class="c"># A real-life example of PowerShell&#39;s Select-String</span>
<span class="nv">$i</span><span class="o">=</span>0
<span class="nv">$File</span> <span class="o">=</span> Get-ChildItem <span class="s2">&quot;C:\html&quot;</span> -include *.html -recurse
<span class="nv">$StringVid</span> <span class="o">=</span> <span class="s2">&quot;msbuild&quot;</span>
foreach <span class="o">(</span><span class="nv">$Entry</span> in <span class="nv">$File</span><span class="o">)</span> <span class="o">{</span>
   <span class="nv">$List</span> <span class="o">=</span> <span class="k">select</span>-string -pattern <span class="nv">$StringVid</span> <span class="nv">$Entry</span>
          <span class="k">if</span> <span class="o">(</span><span class="nv">$List</span>.LongLength -gt 1<span class="o">)</span> <span class="o">{</span>
          <span class="s2">&quot;{0,-8} {1,-4} {2,18}&quot;</span> -f
          <span class="s2">&quot;Files &quot;</span>, <span class="nv">$List</span>.LongLength, <span class="nv">$Entry</span>.FullName;
          <span class="nv">$i</span>++
          <span class="o">}</span>
<span class="o">}</span>
</pre></div>
</div>
</li>
<li><p class="first">Namensauflösung einer IP-Range</p>
<p>Die Programmfunktionalität wird mit Hilfe von 2 Funktionen realisiert.
<strong>Get-ComputerNameByIP</strong> zeigt die Verwendung von Fehlerbehandlungscode,
<strong>Check-Online</strong> zeigt die Verwendung von Hintergrundprozessen.</p>
<p>Als Speicher für die IP-Adressen und Hostnamen wird die Datenstruktur eines <strong>Hashes</strong> verwendet,</p>
<div class="highlight-sh"><div class="highlight"><pre>&lt;<span class="c">#</span>
.SYNOPSIS
Gets a computer name
.DESCRIPTION
Resolve a computer name using an IP address
.PARAMETER &lt;paramName&gt;
<span class="nv">$IPAddress</span>
.EXAMPLE
Get-ComputerNameByIP <span class="s2">&quot;192.168.1.1&quot;</span>
.AUTHOR
G.W. Scheppink
<span class="c">#&gt;</span>

<span class="k">function </span>Get-ComputerNameByIP <span class="o">{</span>
                param<span class="o">(</span>
                        <span class="nv">$IPAddress</span> <span class="o">=</span> <span class="nv">$null</span>
                <span class="o">)</span>

                begin <span class="o">{</span> <span class="o">}</span>

                process <span class="o">{</span>
                        <span class="k">if</span> <span class="o">(</span><span class="nv">$IPAddress</span> -and <span class="nv">$_</span><span class="o">)</span> <span class="o">{</span>
                                Throw <span class="s2">&quot;Please use either pipeline or input parameter&quot;</span>
                                <span class="nb">break</span>
                        <span class="o">}</span> elseif <span class="o">(</span><span class="nv">$IPAddress</span><span class="o">)</span> <span class="o">{</span>
                                <span class="o">([</span>System.Net.Dns<span class="o">]</span>::GetHostbyAddress<span class="o">(</span><span class="nv">$IPAddress</span><span class="o">))</span>
                        <span class="o">}</span> elseif <span class="o">(</span><span class="nv">$_</span><span class="o">)</span> <span class="o">{</span>
                                <span class="nb">trap</span> <span class="o">[</span>Exception<span class="o">]</span> <span class="o">{</span>
                                        write-warning <span class="nv">$_</span>.Exception.Message
                                        <span class="k">continue</span>;
                                <span class="o">}</span>
                                <span class="o">[</span>System.Net.Dns<span class="o">]</span>::GetHostbyAddress<span class="o">(</span><span class="nv">$_</span><span class="o">)</span>
                        <span class="o">}</span> <span class="k">else</span> <span class="o">{</span>
                                <span class="nv">$IPAddress</span> <span class="o">=</span> Read-Host <span class="s2">&quot;Please supply the IP Address&quot;</span>
                                <span class="o">[</span>System.Net.Dns<span class="o">]</span>::GetHostbyAddress<span class="o">(</span><span class="nv">$IPAddress</span><span class="o">)</span>
                        <span class="o">}</span>
                <span class="o">}</span>

                end <span class="o">{</span> <span class="o">}</span>

        <span class="o">}</span> <span class="c"># End function</span>

        <span class="k">function </span>Check-Online <span class="o">{</span>
                param<span class="o">(</span>
                        <span class="nv">$computername</span>
                <span class="o">)</span>

                <span class="nb">test</span>-connection -count 1 -ComputerName <span class="nv">$computername</span> -TimeToLive 5 -asJob |
                Wait-Job |
                Receive-Job |
                Where-Object <span class="o">{</span> <span class="nv">$_</span>.StatusCode -eq 0 <span class="o">}</span> |
                Select-Object -ExpandProperty Address StatusCode
        <span class="o">}</span>

        <span class="c"># This code pings an IP segment from 192.168.1.1 to 192.168.1.254 and returns only those IPs that respond.</span>
        CLS

        <span class="nv">$ip_name</span> <span class="o">=</span> @<span class="o">{}</span>
        <span class="nv">$Start</span> <span class="o">=</span> Get-Date
        <span class="nv">$ips</span> <span class="o">=</span> 1..254 | ForEach-Object <span class="o">{</span> <span class="s2">&quot;192.168.0.$_&quot;</span> <span class="o">}</span>
        <span class="nv">$online</span> <span class="o">=</span> Check-Online -computername <span class="nv">$ips</span>
        <span class="nv">$online</span>

        foreach <span class="o">(</span><span class="nv">$PC</span> in <span class="nv">$online</span><span class="o">)</span> <span class="o">{</span>
                <span class="nv">$pcname</span> <span class="o">=</span>  Get-ComputerNameByIP <span class="nv">$PC</span>
                <span class="nv">$ip_name</span>+<span class="o">=</span>@<span class="o">{</span><span class="nv">$PC</span><span class="o">=</span><span class="nv">$pcname</span>.HostName<span class="o">}</span>
        <span class="o">}</span>

        <span class="nv">$End</span> <span class="o">=</span> Get-Date
        Write-Host <span class="s2">&quot;`nStarted at: &quot;</span> <span class="nv">$Start</span>
        Write-Host <span class="s2">&quot;Ended at: &quot;</span> <span class="nv">$End</span>

        <span class="nv">$ip_name</span>

        <span class="c">#umstÃƒÂ¤ndliches Schreiben der key-value-Paare aus einem Hash heraus</span>
        <span class="nv">$ip_name</span>.GetEnumerator<span class="o">()</span> | Sort-Object Name |
        ForEach-Object <span class="o">{</span><span class="s2">&quot;{0} , {1}&quot;</span> -f <span class="nv">$_</span>.Name,<span class="nv">$_</span>.Value<span class="o">}</span> |
        Add-Content C:<span class="se">\t</span>emp<span class="se">\l</span>og.csv<span class="sb">`</span>
</pre></div>
</div>
</li>
<li><p class="first">Laptop oder Desktop</p>
<blockquote>
<div><p>Sie sind die IT-Leiter einer kleinen mittelstÃƒÂ¤ndischen Firma. Sie mÃƒÂ¼ssen Inventur machen und Sie brauchen dazu die Anzahl der zur Zeit sich im Netz befindlichen Laptop-und Desktop-Computer in ihrer Firma.</p>
<div class="highlight-sh"><div class="highlight"><pre>Following is the full script.

&lt;<span class="c">#</span>
.Synopsis

   helper <span class="k">function </span>used by Get-HardwareType

.EXAMPLE

   Example of how to use this cmdlet
<span class="c">#&gt;</span>
<span class="k">function </span>Is-Laptop <span class="o">{</span>

<span class="o">[</span>CmdletBinding<span class="o">()]</span>
<span class="o">[</span>OutputType<span class="o">([</span>boolean<span class="o">])]</span>

        param
        <span class="o">(</span>
        <span class="o">[</span>Parameter<span class="o">(</span><span class="nv">Mandatory</span><span class="o">=</span><span class="nv">$true</span><span class="o">)]</span>
        <span class="o">[</span>string<span class="o">]</span>        <span class="nv">$strHostName</span>
        <span class="o">)</span>

        <span class="nv">$blnResult</span> <span class="o">=</span> <span class="nv">$false</span>
        <span class="nv">$query</span> <span class="o">=</span> <span class="s2">&quot;select __SERVER,ChassisTypes from Win32_SystemEnclosure&quot;</span>


                <span class="nv">$objWMI</span> <span class="o">=</span> Get-WmiObject -ComputerName <span class="nv">$strHostName</span> -Query <span class="nv">$query</span> -ErrorAction Stop

                switch <span class="o">(</span><span class="nv">$objWMI</span>.ChassisTypes<span class="o">)</span> <span class="o">{</span>

                        9  <span class="o">{</span><span class="nv">$blnResult</span> <span class="o">=</span> <span class="nv">$true</span><span class="o">}</span> <span class="c"># Laptop</span>
                        10 <span class="o">{</span><span class="nv">$blnResult</span> <span class="o">=</span> <span class="nv">$true</span><span class="o">}</span> <span class="c"># Notebook</span>
                        12 <span class="o">{</span><span class="nv">$blnResult</span> <span class="o">=</span> <span class="nv">$true</span><span class="o">}</span> <span class="c"># Docking Station</span>
                        14 <span class="o">{</span><span class="nv">$blnResult</span> <span class="o">=</span> <span class="nv">$true</span><span class="o">}</span> <span class="c"># Sub Notebook</span>
                        default <span class="o">{}</span>
                  <span class="o">}</span>
                  <span class="k">return</span> <span class="nv">$blnResult</span>

<span class="o">}</span> <span class="c"># end function</span>

&lt;<span class="c">#</span>

.Synopsis
  <span class="k">function </span>to determine chassis <span class="nb">type </span>using a WMI query

.DESCRIPTION
   <span class="k">function </span>to determine chassis <span class="nb">type </span>using a WMI query

.EXAMPLE
   <span class="s2">&quot;pc01&quot;</span>,<span class="s2">&quot;pc02&quot;</span>,<span class="s2">&quot;pc03&quot;</span> | Get-HardwareType
<span class="c">#&gt;</span>
<span class="k">function </span>Get-HardwareType <span class="o">{</span>

<span class="o">[</span>CmdletBinding<span class="o">()]</span>
<span class="o">[</span>OutputType<span class="o">([</span>psobject<span class="o">])]</span>

Param
<span class="o">(</span>
<span class="o">[</span>Parameter<span class="o">(</span><span class="nv">Mandatory</span><span class="o">=</span><span class="nv">$true</span>,ValueFromPipeline<span class="o">=</span><span class="nv">$true</span><span class="o">)]</span>
<span class="nv">$strHostName</span>
<span class="o">)</span>

process <span class="o">{</span>

        try <span class="o">{</span>

                <span class="nv">$objHostName</span> <span class="o">=</span> <span class="o">[</span>system.net.dns<span class="o">]</span>::GetHostByName<span class="o">(</span><span class="nv">$strHostName</span><span class="o">)</span>

                <span class="nv">$query</span> <span class="o">=</span> <span class="s2">&quot;select __SERVER,ChassisTypes from Win32_SystemEnclosure&quot;</span>

                        <span class="k">if</span> <span class="o">(</span>Test-Connection -ComputerName <span class="nv">$objHostName</span>.HostName -count 1 -erroraction silentlycontinue<span class="o">)</span> <span class="o">{</span>

                                try <span class="o">{</span>

                                        <span class="nv">$objResult</span> <span class="o">=</span> New-Object -TypeName psobject -Property @<span class="o">{</span><span class="nv">HostName</span><span class="o">=</span><span class="nv">$objHostName</span>.HostName;IsLaptop<span class="o">=(</span>Is-Laptop -strHostName <span class="nv">$objHostName</span>.HostName<span class="o">)}</span>

                                        <span class="k">return</span> <span class="nv">$objResult</span>

                                <span class="o">}</span>

                                catch <span class="o">{</span>

                                        <span class="s2">&quot;Error trying to query $($objHostName.HostName)&quot;</span>

                                <span class="o">}</span>

                   <span class="o">}</span>

                   <span class="k">else</span> <span class="o">{</span>

                                write-host <span class="s2">&quot;error connecting to $($objHostName.HostName)&quot;</span>

                   <span class="o">}</span>

        <span class="o">}</span>

        catch <span class="o">{</span>

                write-host <span class="s2">&quot;Unable to resolve DNS address for $strHostName&quot;</span>

        <span class="o">}</span>

  <span class="o">}</span>

<span class="o">}</span> <span class="c"># end function</span>


<span class="nv">$laptopCount</span> <span class="o">=</span> 0
<span class="nv">$desktopCount</span> <span class="o">=</span> 0

<span class="nv">$searcher</span> <span class="o">=</span> new-object directoryservices.directorysearcher<span class="o">([</span>ADSI<span class="o">]</span><span class="s2">&quot;&quot;</span>,<span class="s2">&quot;(&amp;(objectcategory=computer)(!operatingsystem=*server*))&quot;</span><span class="o">)</span>

<span class="o">[</span>void<span class="o">]</span><span class="nv">$searcher</span>.PropertiesToLoad.Add<span class="o">(</span><span class="s2">&quot;cn&quot;</span><span class="o">)</span>
<span class="nv">$arrMachineName</span> <span class="o">=</span> <span class="nv">$searcher</span>.findall<span class="o">()</span> | %<span class="o">{</span><span class="nv">$_</span>.properties.cn<span class="o">}</span>

<span class="nv">$result</span> <span class="o">=</span> <span class="nv">$arrMachineName</span> | Get-HardwareType
<span class="nv">$result</span>

<span class="nv">$result</span> | ForEach-Object <span class="o">{</span><span class="k">if</span> <span class="o">(</span><span class="nv">$_</span>.islaptop<span class="o">)</span> <span class="o">{</span><span class="nv">$laptopCount</span>++<span class="o">}</span> <span class="k">else</span> <span class="o">{</span><span class="nv">$desktopCount</span>++<span class="o">}}</span>

<span class="s2">&quot;Laptop Total: $laptopCount&quot;</span>
<span class="s2">&quot;Desktop Total: $desktopCount&quot;</span>

<span class="c">## beim Lesen aus Textdatei</span>
<span class="c">#$result = import-csv C:\temp\laptop_desktop.ps1 | Group-Object {$_.chassis} -noelement</span>
<span class="c">#$result = import-csv C:\temp\laptop_desktop.ps1 | Group-Object -property chassis -noelement</span>

<span class="c">#$result</span>
</pre></div>
</div>
</div></blockquote>
</li>
</ul>
</div>
<div class="section" id="objekte">
<h2>14.11. Objekte<a class="headerlink" href="#objekte" title="Permalink zu dieser Überschrift">¶</a></h2>
<div class="section" id="aufgabe-1">
<h3>14.11.1. Aufgabe 1:<a class="headerlink" href="#aufgabe-1" title="Permalink zu dieser Überschrift">¶</a></h3>
<div class="sidebar">
<p class="first sidebar-title">Arbeitsblatt</p>
<ul class="last simple">
<li><a class="reference download internal" href="_downloads/objekte_aufgabe11.pdf"><tt class="xref download docutils literal"><span class="pre">A1</span> <span class="pre">Unkommentiert</span></tt></a>.</li>
<li><a class="reference download internal" href="_downloads/objekte_aufgabe1_comment1.pdf"><tt class="xref download docutils literal"><span class="pre">A1</span> <span class="pre">Kommentiert</span></tt></a>.</li>
</ul>
</div>
<div class="highlight-sh"><div class="highlight"><pre> <span class="nv">$strComputer</span> <span class="o">=</span> <span class="s2">&quot;.&quot;</span>

 <span class="c"># Neues leeres Objekt erstellen</span>
 <span class="nv">$Infos</span><span class="o">=</span> New-object -TypeName PSObject

 <span class="c"># Wert an das Objekt anfügen</span>
 Add-Member -InputObject <span class="nv">$Infos</span> -Name Manufacturer -Value 1 -MemberType NoteProperty
 <span class="c"># Wert an das Objekt anfügen</span>
 Add-Member -InputObject <span class="nv">$Infos</span> -Name Model -Value 2 -MemberType NoteProperty
 <span class="c"># Wert an das Objekt anfügen</span>
 Add-Member -InputObject <span class="nv">$Infos</span> -Name Memory -Value 3 -MemberType NoteProperty
 Add-Member -InputObject <span class="nv">$Infos</span> -Name BiosDescription -Value 3 -MemberType NoteProperty
 Add-Member -InputObject <span class="nv">$Infos</span> -Name SerialNumber -Value 3 -MemberType NoteProperty
 Add-Member -InputObject <span class="nv">$Infos</span> -Name OperatingSystem -Value 3 -MemberType NoteProperty

  Function SysInfo<span class="o">(</span><span class="nv">$Infos</span><span class="o">)</span>
  <span class="o">{</span>
      <span class="nv">$colItems</span> <span class="o">=</span> Get-WmiObject Win32_ComputerSystem <span class="sb">`</span>
      -Namespace <span class="s2">&quot;root\CIMV2&quot;</span> -ComputerName <span class="nv">$strComputer</span>

      foreach<span class="o">(</span><span class="nv">$objItem</span> in <span class="nv">$colItems</span><span class="o">)</span> <span class="o">{</span>
        <span class="nv">$Infos</span>.Manufacturer <span class="o">=</span>  <span class="nv">$objItem</span>.Manufacturer
        <span class="nv">$Infos</span>.Model <span class="o">=</span> <span class="nv">$objItem</span>.Model
       <span class="nv">$Infos</span>.Memory <span class="o">=</span>  <span class="nv">$objItem</span>.TotalPhysicalMemory
      <span class="o">}</span>
   <span class="o">}</span>

   Function BIOSInfo<span class="o">(</span><span class="nv">$Infos</span><span class="o">)</span>
   <span class="o">{</span>

      <span class="nv">$colItems</span> <span class="o">=</span> Get-WmiObject Win32_BIOS -Namespace <span class="s2">&quot;root\CIMV2&quot;</span> <span class="sb">`</span>
      -computername <span class="nv">$strComputer</span>
      foreach<span class="o">(</span><span class="nv">$objItem</span> in <span class="nv">$colItems</span><span class="o">)</span> <span class="o">{</span>
              <span class="nv">$Infos</span>.BiosDescription <span class="o">=</span> <span class="nv">$objItem</span>.Description
              <span class="nv">$Infos</span>.SerialNumber <span class="o">=</span>  <span class="nv">$objItem</span>.SerialNumber
      <span class="o">}</span>
   <span class="o">}</span>


   Function OSInfo<span class="o">(</span><span class="nv">$Infos</span><span class="o">)</span>
   <span class="o">{</span>
      <span class="nv">$colItems</span> <span class="o">=</span> Get-WmiObject Win32_OperatingSystem -Namespace <span class="sb">`</span> <span class="s2">&quot;root\CIMV2&quot;</span> -Computername <span class="nv">$strComputer</span>

      foreach<span class="o">(</span><span class="nv">$objItem</span> in <span class="nv">$colItems</span><span class="o">)</span> <span class="o">{</span>
              <span class="nv">$Infos</span>.OperatingSystem <span class="o">=</span>  <span class="nv">$objItem</span>.Name
      <span class="o">}</span>
  <span class="o">}</span>



 <span class="c">#*=============================================================</span>
 <span class="c">#* SCRIPT BODY</span>
 <span class="c">#*=============================================================</span>
 <span class="c">#* Connect to computer</span>


 <span class="c">#* Call SysInfo Function</span>
 Write-Host <span class="s2">&quot;Sytem Information&quot;</span>
 SysInfo <span class="nv">$Infos</span>


<span class="c">#* Call BIOSinfo Function</span>
Write-Host <span class="s2">&quot;System BIOS Information&quot;</span>
BIOSInfo <span class="nv">$Infos</span>
Write-Host

<span class="c">#* Call OSInfo Function</span>
Write-Host <span class="s2">&quot;Operating System Information&quot;</span>
OSInfo <span class="nv">$Infos</span>
Write-Host


<span class="nv">$Infos</span> | Select-Object Name, Model

<span class="c"># CSV Export der Daten</span>
Export-Csv -InputObject <span class="nv">$Infos</span> -Path <span class="s2">&quot;C:\Temp\infos.csv&quot;</span>

 <span class="sb">`</span>
</pre></div>
</div>
</div>
<div class="section" id="aufgabe-2">
<h3>14.11.2. Aufgabe 2:<a class="headerlink" href="#aufgabe-2" title="Permalink zu dieser Überschrift">¶</a></h3>
<div class="sidebar">
<p class="first sidebar-title">Arbeitsblatt</p>
<ul class="last simple">
<li><a class="reference download internal" href="_downloads/objekte_aufgabe21.pdf"><tt class="xref download docutils literal"><span class="pre">A2</span> <span class="pre">Unkommentiert</span></tt></a>.</li>
<li><a class="reference download internal" href="_downloads/objekte_aufgabe2_comment1.pdf"><tt class="xref download docutils literal"><span class="pre">A2</span> <span class="pre">Kommentiert</span></tt></a>.</li>
</ul>
</div>
<div class="highlight-csharp"><pre>Measure-Command{
$site_urls = Import-CSV C:\temp\weekly_stats.csv

function bla
{

    foreach($url in $site_urls)
    {
        $obj_avg = New-Object -TypeName psobject
        Add-Member -InputObject $obj_avg -MemberType NoteProperty -Name "site_url" -Value $url.site_url
        Add-Member -InputObject $obj_avg -MemberType NoteProperty -Name "url_avg" `
            -Value ([int](([int]$url.monday + [int]$url.tuesday + [int]$url.wednesday + [int]$url.thursday + [int]$url.friday + [int]$url.saturday + [int]$url.sunday)/7))
        $obj_avg
    }

}

bla | Sort-Object url_avg

}




Measure-Command {
$site_urls2 = Import-CSV C:\temp\weekly_stats.csv

$site_urls2 | Select @{name="site_url";expression={$_.site_url}}, `
@{name="avg";expression={([int](([int]$_.monday + [int]$_.tuesday + [int]$_.wednesday + [int]$_.thursday + [int]$_.friday + [int]$_.saturday + [int]$_.sunday)/7))}} | `
Sort-Object avg
}







add-type @"
using System;
public class siteurl{

        public string url = "";
        public double durchschnitt = 0;

        public void seturl(string _url)
        {
            this.url = _url;
        }

        public void avg(int mon, int tue, int wed, int thu, int fri, int sat, int sun)
        {
                this.durchschnitt =  ((monday+tuesday+wednesday+ thursday + friday+ saturday + sunday)/7);
        }
}
"@

Measure-Command {
function bla2
{

$site_urls = Import-CSV C:\temp\weekly_stats.csv

    foreach($url in $site_urls)
    {
        $net_avg = New-Object -typename siteurl
        $net_avg.seturl($url.site_url)
        $net_avg.avg($url.monday,$url.tuesday,$url.wednesday,$url.thursday,$url.friday,$url.saturday,$url.sunday)
        $net_avg
    }

}

bla2 |select url, durchschnitt | Sort-Object durchschnitt
 `</pre>
</div>
</div>
<div class="section" id="aufgabe-3">
<h3>14.11.3. Aufgabe 3:<a class="headerlink" href="#aufgabe-3" title="Permalink zu dieser Überschrift">¶</a></h3>
<p>Gegeben ist folgendes PS-Skript.</p>
<div class="highlight-sh"><div class="highlight"><pre>Function Get-NetworkConfiguration
<span class="o">{</span>
    param <span class="o">(</span>
        <span class="o">[</span>parameter<span class="o">(</span>
            <span class="nv">ValueFromPipeline</span><span class="o">=</span><span class="nv">$true</span>,
            <span class="nv">ValueFromPipelineByPropertyName</span><span class="o">=</span><span class="nv">$true</span>,
            <span class="nv">Position</span><span class="o">=</span>0<span class="o">)]</span>
        <span class="o">[</span>Alias<span class="o">(</span><span class="s1">&#39;__ServerName&#39;</span>, <span class="s1">&#39;Server&#39;</span>, <span class="s1">&#39;Computer&#39;</span>, <span class="s1">&#39;Name&#39;</span><span class="o">)]</span>
        <span class="o">[</span>string<span class="o">[]]</span>
        <span class="nv">$ComputerName</span> <span class="o">=</span> <span class="nv">$env</span>:COMPUTERNAME,
        <span class="o">[</span>parameter<span class="o">(</span><span class="nv">Position</span><span class="o">=</span>1<span class="o">)]</span>
        <span class="o">[</span>System.Management.Automation.PSCredential<span class="o">]</span>
        <span class="nv">$Credential</span>
    <span class="o">)</span>
    process
    <span class="o">{</span>
        <span class="nv">$WMIParameters</span> <span class="o">=</span> @<span class="o">{</span>
</pre></div>
</div>
<ul>
<li><p class="first">Wählen Sie statt der Write-Host-Ausgabe einen objektorientieten Ansatz</p>
<p>Der foreach-teil muss wie folgt geändert werden:</p>
<div class="highlight-sh"><div class="highlight"><pre>foreach <span class="o">(</span><span class="nv">$adapter</span> in <span class="o">(</span>Get-WmiObject @WMIParameters<span class="o">))</span>
<span class="o">{</span>
    <span class="nv">$AdapterProperties</span> <span class="o">=</span> @<span class="o">{</span>
        <span class="nv">Server</span> <span class="o">=</span> <span class="nv">$adapter</span>.DNSHostName
        <span class="nv">Adapter</span> <span class="o">=</span>  <span class="nv">$adapter</span>.Description
        <span class="nv">IPAddress</span> <span class="o">=</span> <span class="nv">$adapter</span>.IpAddress
        <span class="nv">SubnetMask</span> <span class="o">=</span> <span class="nv">$adapter</span>.IPSubnet
        <span class="nv">DefaultGateway</span> <span class="o">=</span> <span class="nv">$adapter</span>.DefaultIPGateway
        <span class="nv">DNSServers</span> <span class="o">=</span> <span class="nv">$adapter</span>.DNSServerSearchOrder
        <span class="nv">DNSDomain</span> <span class="o">=</span> <span class="nv">$adapter</span>.DNSDomain
    <span class="o">}</span>

    New-Object PSObject -Property <span class="nv">$AdapterProperties</span>
<span class="o">}</span>
</pre></div>
</div>
</li>
<li><p class="first">Sie wollen nicht alle Informationen ausgeben, sondern beispielsweise nur die IP-Adresse und die Subnet-Maske</p>
<blockquote>
<div><div class="highlight-sh"><div class="highlight"><pre>Get-NetworkConfiguration ‘Server1’, ‘Server2’, ‘Server3’ | Format-Table Server, Adapter, SubnetMask, DefaultGateway –auto –wrap
</pre></div>
</div>
</div></blockquote>
</li>
<li><p class="first">Können Sie mit diesem Skript mehrere Rechner abfragen ?</p>
<p>ja</p>
</li>
<li><p class="first">Welche Rechner ihres Netzes benutzen den gleichen DNS-Server mit der
IP-Adresse <strong>*.*</strong>.***.***. Eine Liste der Computer erhalten Sie mit Hilfe des CommandLets get-adcomputer</p>
<div class="highlight-sh"><div class="highlight"><pre>Get-ADComputer –filter * | Get-NetworkConfiguration | Where-Object <span class="o">{</span><span class="nv">$_</span>.DNSServers –contains <span class="nv">$IPofMyTroublesomeDNSServer</span><span class="o">}</span>
</pre></div>
</div>
</li>
<li><p class="first">Wie viele Rechner benutzen den gleichen Default-Gateway</p>
<div class="highlight-sh"><div class="highlight"><pre>Get-ADComputer -filter * | Get-NetworkConfiguration | Group-Object DefaultGateway -NoElement
</pre></div>
</div>
</li>
</ul>
</div>
</div>
<div class="section" id="fehlerbehandlung">
<h2>14.12. Fehlerbehandlung<a class="headerlink" href="#fehlerbehandlung" title="Permalink zu dieser Überschrift">¶</a></h2>
<span class="target" id="loesung-fehlerbehandlung"></span></div>
</div>


          </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="Allgemeiner Index"
             >Index</a></li>
        <li class="right" >
          <a href="powershell_uml.html" title="15. UML"
             >weiter</a> |</li>
        <li class="right" >
          <a href="Aufgaben.html" title="13. Aufgaben"
             >zurück</a> |</li>
        <li><a href="index.html">PS4SYS</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2010, STE.
      Mit <a href="http://sphinx-doc.org/">Sphinx</a> 1.2b1 erstellt.
    </div>
  </body>
</html>